I’ve noticed that when I have an error in a fetch request (bad URL, etc.), npx execute will hang indefinitely waiting for the promise to resolve. I can’t see or output an error message.
I’ve had this happen in other packs and resolved it by eyeballing the code and fixing it without any kind of debugging.
Even if I add a try/catch, it still hangs.
Here’s a simple example:
pack.addNetworkDomain("raindrop.io");
.
.
.
execute: async function ([], context) {
console.log("Getting collections");
try {
let response = await context.fetcher.fetch({
method: "GET",
url: "https://api.raindrop.io/rest/v1/collections",
});
console.log(response.body);
let results = [];
for (let collection of response.body.items) {
results.push(toCollection(collection));
}
return {
result: results,
};
} catch (error) {
console.log(error);
}
},
I’m not even close to being a NodeJS expert, but I think the deep answer to your question is in this presentation. It’s deep, but very informative about the underlying event loop in Node.
Node keeps track of the active things happening in your code, and when there is nothing else happening, it simply exits. I think this is what you may be seeing; a promise that never resolves. As such, a try-catch will not see that which is unresolved.
As I’ve gleaned from far smarter developers than muself, Node keeps a reference count of things like timers and network requests. When you make a network, or other async request, set a timer, etc. Node adds on to this ref count. When the times/request resolve, Node subtracts from the count. This count is how Node decides whether to exit at the end of the event loop. When you get to the end of the event loop Node looks at this count and if it’s zero exits.
How long does run? 300 seconds? I believe that’s the default for Chrome, but could be different depending on other browsers. And, I’m uncertain if this isn’t a server-side fetch anyway.
Still way over my head, but maybe consider this:
async function loadRaindrop() {
try {
const response = await fetchWithTimeout('/rest/v1/collections', {
timeout: 6000
});
const games = await response.json();
return games;
} catch (error) {
// Timeouts if the request takes
// longer than 6 seconds
console.log(error.name === 'AbortError');
}
}
@Troy_Larson - Thanks for reporting this. I can’t seem to get npx execute to hang like you are describing, so it’s hard to know what the cause is. If you can narrow it down at all let me know.
I ran into this issue, running npx coda execute on a terminal window in VS Code (in case it matters). After trying a bunch of different ways to wait on promises, and all of them hanging Eric suggested that I run the command with the --vm=false flag set, and that made it stop hanging and work as expected.
Your results may vary and I can’t say definitely this is the solution but I thought I would post what helped me. I hope it fixes your problem as well.
Diving in deeper, it seems like something broke when the VM package we are using (isolated-vm) released their latest version. I’ve raised the issue with our engineering team, but in the mean time disabling the VM is a good workaround.