我有这个node.js函数process(),它应该在调用时返回一个值。我在为我的process()创建回调时遇到问题。该值仅应在从ec2.runInstances(params,function(err,data)调用)获得响应后才从process()返回。
-------------来自app.js(express.js)的代码段--------------------
var createEngine = require('./ec2_create.js');
app.get('/create', function (req, res) {
res.render('create', {
status: createEngine.process()
});
});
------------------------来自ec2_create.js的代码段-------------------- ---
function process(callback) {
var status = null;
// Create the instance
ec2.runInstances(params, function (err, data) {
if (err) {
//console.log("Could not create instance", err);
status = "Could not create instance: " + err;
} else {
var instanceId = data.Instances[0].InstanceId;
//console.log("Created instance", instanceId);
status = "Created instance: " + instanceId;
}
});
callback(status);
};
module.exports.process = process;
尝试如下
function ec2process(callback){
var status = null;
// Create the instance
ec2.runInstances(params, function(err, data) {
if (err) {
//console.log("Could not create instance", err);
status = "Could not create instance: " + err;
}
else{
var instanceId = data.Instances[0].InstanceId;
//console.log("Created instance", instanceId);
status = "Created instance: " + instanceId;
}
callback(status); // Callback moved
});
};
出口。进程= ec2process
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句