laverdet / isolated-vm

Secure & isolated JS environments for nodejs
ISC License
2.15k stars 152 forks source link

Wait for promises in an isolate to finish #268

Open JSH32 opened 2 years ago

JSH32 commented 2 years ago

Currently, when a Promise is still running in a script, it will end without waiting for them to finish. Is there a way to wait for all promises to finish and for the event loop to be before returning from evaluate on module? Example of usage

Isolate code:

testFetch().then(console.log)

Node code:

await context.global.set("testFetch", new ivm.Callback(async () => {
    console.log("called")
    return await axios.get("https://google.com")
}, { async: true }))

called does print in my console and attempting to print testFetch itself does indeed return a pending Promise, but attempting to await the promise does nothing. It simply never resolves and the console.log in the .then never gets called

JSH32 commented 2 years ago

Well I figured out my stupidity. Now I pass the resolve/reject functions to the function which is non async and call them when fetch is done. Out of curiosity would there be a way to natively support async callbacks?