如何在async.each中包含async.waterfall的外部函数中调用async.each
我在Node.JS中有一个出口模块
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.each
embeddedasync.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 }
我得到的输出是
在第一个回叫
称为外部function
计算位置#
计算位置#
计算内在的第一瀑布
计算内在的第一瀑布
返回值被处理
在第二个回叫
返回值是== NaN
我希望一切按以下顺序同步运行。
在Demo.findone的execcallback中调用async.waterfall
调用firstOrderFunction
在firstOrderFunction中调用async.each
在async.each中调用async.waterfall
调用返回a = 14的第一个callback函数。
调用第二个callback函数返回c = 14 * 14 = 196。
我如何使用asynchronous实现这一点?
提前致谢,并道歉这么长的一个问题。
在async.waterfall()的末尾调用async.each()的callback函数,并在async.each()的末尾调用firstOrderFunction的callback函数。 这里是修改后的代码:
function fistOrderFunction(demosReturned, callback){ var ret = []; console.log("Called the External Function"); async.each(demosReturned.locations, function(location, eachCb) { console.log('Computing Location #'); async.waterfall([ function(waterfallCb){ console.log("Computing Inner First Waterfall"); a = 14; waterfallCb(null,a); }, function(a,waterfallCb){ console.log("Computing Inner Second Waterfall"); b =14; c = a*b; waterfallCb(null,c) } ],function(err,c){ if(err){ console.log("Error is == " +err); eachCb(err); }else{ ret.push(c); console.log("The Returned Value is == "+c); eachCb(null); } });//End Async.Waterfall },function(err){ if(err){ console.log("The Error in Async.Each === " + err); callback(err, null); }else{ console.log("The Returned Value is Processed "); callback(null, ret); } }); //End Async.Each }
- TypeError:任务不是asynchronousjs平行的函数
- 尝试遍历一个数组并为每个数组添加一个get函数
- stream节点fluent-ffmpegstreammp4video
- Jade模板引擎 – 每个迭代偏移量
- 清理closuresexpress.js + mongodb服务器(使用node.js)
- 我可以使用APNauthentication密钥.p8文件与PHP发送iOS推送通知?
- 奇怪的“超时”,当试图从Sails.js应用程序执行MongoDB查询
- 如何使用swift 3 for iOS连接到MEAN stack REST api
- 在Express.js中使用traceId进行logging