You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript includes a setTimeout function: yes
Functions are a type of primitive value in JavaScript: yes
Objects can be shared between isolates: no
Is there a way to wait for an async module to execute?
importivmfrom"isolated-vm";constisolate=newivm.Isolate();constcontext=awaitisolate.createContext();constmodule=awaitisolate.compileModule(/* language=javascript */` export const x = 10; await new Promise(resolve => globalThis.hook = resolve); export const y = 20;`);awaitmodule.instantiate(context,()=>null);constmoduleEvalPromise=module.evaluate({promise: true,reference: true,timeout: 10});consthook=awaitcontext.global.get("hook");setTimeout(hook,100);moduleEvalPromise.then(async(moduleValue)=>{console.log("Module evaluated",moduleValue);console.log("Value-X",awaitmodule.namespace.get("x"));console.log("Value-Y",awaitmodule.namespace.get("y"));// error});
I get an error when accessing variable y
Module evaluated undefined
Value-X 10
node:internal/process/promises:394
triggerUncaughtException(err, true /* fromPromise */);
^
ReferenceError: y is not defined
at (<isolated-vm boundary>)
at main.mjs:18:51
v8 and JavaScript added top-level await after the implementation in isolated-vm, so this is probably just a limitation of the current implementation. I've been working on a rewrite but obviously that won't help you now, since that won't be ready for quite some time. You can probably work around it today by injecting a promise resolution at the end of the module and waiting on that instead.
JavaScript includes a setTimeout function: yes
Functions are a type of primitive value in JavaScript: yes
Objects can be shared between isolates: no
Is there a way to wait for an async module to execute?
I get an error when accessing variable
y
But this code wotks fine:
I think
async module.evaluate
resolves before the module is executed.Is it possible to get Promise or callback when the module will be executed to the end?
The text was updated successfully, but these errors were encountered: