2015-04-01 21 views
1

外部函数调用我的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 
}; 

有一个出口模块现在,我再次firstOrderfunctionasync.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

    1. 呼叫async.waterfall async.each

    2. 调用返回a = 14的第一个回调函数。

    3. 调用返回c = 14 * 14 = 196的第二个回调函数。

    如何做到这一点使用异步?

    在此先感谢和道歉这么长的问题。

  • 回答

    1

    在async.waterfall()的末尾调用async.each()的回调函数,并在async.each()的末尾调用firstOrderFunction的回调函数。这里是修改的代码:

    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 
    } 
    
    +0

    完美!奇迹般有效。我试图将'ret'传递给'async.each'回调函数。所以'ret'超出了范围,因为'async.waterfall'已经完成执行。非常感谢。干杯! :) – theChinmay 2015-04-06 08:34:29