How to deal with promises in loop?

Viewed 3122

This is what I would like to do

var response = [];

Model.find().then(function(results){
   for(r in results){
      MyService.getAnotherModel(results[r]).then(function(magic){
          response.push(magic);
      });          
   }
});

//when finished
res.send(response, 200);

however it returns just [] because the async stuff is not ready yet. I am using sails.js that uses Q promise. Any ideas how to return a response when all async calls are finished?

https://github.com/balderdashy/waterline#query-methods (promise methods)

3 Answers
Related