Node.js – wait for multiple async calls

I’m a big fan of underscore/lodash, so I usually use _.after, which creates a function that only executes after being called a certain number of times.

var finished = _.after(2, doRender);

asyncMethod1(data, function(err){
  //...
  finished();
});

asyncMethod2(data, function(err){
  //...
  finished();
})

function doRender(){
  res.render(); // etc
} 

Since javascript hoists the definition of functions defined with the function funcName() syntax, your code reads naturally: top-to-bottom.

Leave a Comment