I'm working on my first nodejs application which is using bluebird. I've a requirement to call an asynchronous method inside a for loop which returns a promise for each call. I want to combine all those promises together, how can I achieve this. Any help is much appreciated.
I'm retrieving set of documents from mongodb and I need to loop through that returned set of documents and pass a field as a parameter to an asynchronous method which returns a promise. Since i'm doing this in the loop I need a way to combine all of those promises in to one single promise.
You can use the .all()
method : http://bluebirdjs.com/docs/api/promise.all.html
Example :
var promises = [];
for (var i = 0; i < 100; ++i) {
promises.push(yourPromise);
}
Promise.all(promises).then(function(values) {
// All your promises are resolved
// Promises results are stored in values argument
console.log(values);
});