外部函数调用我的Node.js如何async.each具有async.waterfall内async.each
exports.doSomethingImportant= function(req, res) {
var id = req.params.id;
Demo.findOne({'_id': id})
.exec(function(err, demosReturned) {
async.waterfall([
function(outerCallBack){
console.log("In the First Call Back");
firstOrderFunction(demosReturned,outerCallBack);
},
function(x,outerCallBack){
var y =3
var z = x*y;
console.log("In the Second Call Back");
outerCallBack(null,z);
}
],function(err,z){
if(err){
console.log("Error is == " +err);
}else{
console.log("The Returned Value is == "+z);
}
});
});//End Demo.findOne
};
有一个出口模块现在,我再次firstOrderfunction
有async.each
嵌入async.waterfall
function fistOrderFunction(demosReturned,outerCallBack){
console.log("Called the External Function");
async.each(demosReturned.locations, function(location, innerCallBack) {
console.log('Computing Location #');
async.waterfall([
function(internalCallBack){
console.log("Computing Inner First Waterfall");
a = 14;
innternalCallBack(null,a);
},
function(a,internalCallBack){
console.log("Computing Inner Second Waterfall");
b =14;
c = a*b;
innternalBack(null,c)
}
],function(err,c){
if(err){
console.log("Error is == " +err);
}else{
d = c;
console.log("The Returned Value is == "+c);
innerCallBack(null,d);
}
});//End Async.Waterfall
},function(err,d){
if(err){enter code here
console.log("The Error in Async.Each === " + err);
}else{
console.log("The Returned Value is Processed ");
outerCallBack(null, d);
}
}); //End Async.Each
}
我得到的输出是
在第一次调用返回
调用的外部函数
计算位置#
计算位置#
计算内蒙古第一瀑
计算内蒙古第一瀑
的退货价值已处理
在第二次调用返回
返回值== NaN的
我希望一切按以下顺序同步运行。在exec调用回Demo.findone
的调用内部firstOrderFunction
呼叫async.waterfall内firstOrderFunction
呼叫async.each
呼叫async.waterfall async.each
调用返回a = 14的第一个回调函数。
调用返回c = 14 * 14 = 196的第二个回调函数。
如何做到这一点使用异步?
在此先感谢和道歉这么长的问题。
完美!奇迹般有效。我试图将'ret'传递给'async.each'回调函数。所以'ret'超出了范围,因为'async.waterfall'已经完成执行。非常感谢。干杯! :) – theChinmay 2015-04-06 08:34:29