Skip to content

Instantly share code, notes, and snippets.

@seromenho
Created February 13, 2020 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seromenho/b23bfbabdefbbaa19ea7865b7af9a174 to your computer and use it in GitHub Desktop.
Save seromenho/b23bfbabdefbbaa19ea7865b7af9a174 to your computer and use it in GitHub Desktop.
Get Node VM results from an async script
const vm = require('vm');
(async () => {
const sandbox = {
a: 1
};
await new Promise(resolve => {
sandbox.resolve = resolve;
const code = 'Promise.resolve(2).then(result => {a = result; resolve();})';
const script = new vm.Script(code);
const context = new vm.createContext(sandbox);
script.runInContext(context);
});
console.log(sandbox.a); // 2
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment