There are many control flow libraries. I used Q in my previous projects, which I have no complaints about, however, I will probably deal with using the async caolan library for my next project.
https://github.com/caolan/async
From what you described above, you probably want to look at using a parallel function
https://github.com/caolan/async#parallel
The problem you described can be easily transferred to a parallel example in documents
EDIT: I missed a bit about API dependencies. Whenever you need to pass values ββin a chain and control the order, you need to use the waterfall method (see qiao answer). If there is a case where the calls are independent, you should use the parallel method. Example parallel method below
async.parallel({ google: function(callback){ http.get("http://www.google.com", function(res){ console.log("google done"); callback(null, res.statusCode); }) }, yahoo: function(callback){ http.get("http://www.yahoo.com", function(res){ console.log("yahoo done"); callback(null, res.statusCode); }) } }, function(err, results) { if(!err){ console.log("all done"); console.log(results.google); console.log(results.yahoo); }else{ console.log(err); } } );
It does all your queries in parallel and gives you a callback when all this is done. Here you will massage your data.
List of control flow libraries:
https://github.com/joyent/node/wiki/Modules#wiki-async-flow
wdavo
source share