javascript - How to call async.each in an external function having async.waterfall inside async.each -


i have exports module in 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 first call back");           firstorderfunction(demosreturned,outercallback);         },         function(x,outercallback){            var y =3             var z = x*y;            console.log("in second call back");            outercallback(null,z);         }       ],function(err,z){         if(err){           console.log("error == " +err);         }else{           console.log("the returned value == "+z);         }       }); });//end demo.findone }; 

now, firstorderfunction again has async.each embedding async.waterfall

function fistorderfunction(demosreturned,outercallback){ console.log("called external function");   async.each(demosreturned.locations, function(location, innercallback) {   console.log('computing location #');      async.waterfall([           function(internalcallback){              console.log("computing inner first waterfall");                = 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 == " +err);         }else{              d = c;              console.log("the returned value == "+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 processed ");       outercallback(null, d);     } }); //end async.each } 

the output


in first call back

called external function

computing location #

computing location #

computing inner first waterfall

computing inner first waterfall

the returned value processed

in second call back

the returned value == nan

i want run synchronously in following order.

  1. call async.waterfall in exec call of demo.findone

  2. call firstorderfunction

  3. call async.each inside firstorderfunction

  4. call async.waterfall inside async.each

  5. call first callback function returning a=14.

  6. call second callback function returning c =14*14 =196.

how achieve using async?

thanks in advance , apology such long question.

call async.each()'s callback @ end of async.waterfall() , call firstorderfunction's callback @ end of async.each(). here revised code:

function fistorderfunction(demosreturned, callback){   var ret = [];    console.log("called external function");   async.each(demosreturned.locations, function(location, eachcb) {     console.log('computing location #');      async.waterfall([         function(waterfallcb){             console.log("computing inner first waterfall");             = 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 == " +err);             eachcb(err);         }else{             ret.push(c);             console.log("the returned value == "+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 processed ");         callback(null, ret);     }   }); //end async.each } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -