3

这是我的代码

var offer = {title:'fdsfds',price:323,value:140};
    var initSandbox = {
            http: http,
            offer: offer
        };
var context = vm.createContext(initSandbox);
vm.runInContext(code, context);

当 runIncontext 在提供的上下文中成功执行代码时,它会在上下文中更新提供对象,因此我需要知道它何时完成执行,以便我可以从上下文中检索提供对象。

4

1 回答 1

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);
于 2012-06-21T08:30:33.440 回答