After calling two asynchronous requests using mongoose

Using mongoose, I would like to have a callback after completing two different requests.

  var team = Team.find ({name: 'myteam'});
 var games = Game.find ({visitor: 'myteam'});

Then how to link and / or wrap these 2 requests in promises if I want these requests not to be blocked and executed asynchronously?

I would like to avoid the following lock code:

  team.first (function (t) {
   games.all (function (g) {
     // Do something with t and g
   });
 });
+4
source share
2 answers

I think you have already found a solution, but anyway. You can easily use the async library. In this case, your code will look like this:

async.parallel( { team: function(callback){ Team.find({name: 'myteam'}, function (err, docs) { callback(err, docs); }); }, games: function(callback){ Games.find({visitor: 'myteam'}, function (err, docs) { callback(err, docs); }); }, }, function(e, r){ // can use r.team and r.games as you wish } ); 
+12
source

I think you want to look at something like

https://github.com/creationix/step

+1
source

All Articles