这是我的代码
var offer = {title:'fdsfds',price:323,value:140};
var initSandbox = {
http: http,
offer: offer
};
var context = vm.createContext(initSandbox);
vm.runInContext(code, context);
当runIncontext在提供的上下文中成功执行代码时,它会在上下文中更新提供对象,因此我需要知道它何时完成它的执行,以便我可以从上下文中检索商品对象。
答案 0 :(得分:6)
将回调传递给您的上下文并从您的代码中触发
var vm = require('vm');
var offer = {title:'fdsfds',price:323,value:140};
var initSandbox = {
http: 123,
offer: offer,
done : function(offer) {
console.log('done!');
},
setTimeout: setTimeout
};
var context = vm.createContext(initSandbox);
vm.runInContext("setTimeout(done,100)", context);