Created
February 13, 2020 21:27
-
-
Save seromenho/b23bfbabdefbbaa19ea7865b7af9a174 to your computer and use it in GitHub Desktop.
Get Node VM results from an async script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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