Devz Devz - 3 years ago 135
Node.js Question

Bluebird - Mixing each() and all()?

I've just started using Bluebird and it's really great ! But i was wondering if i could do the following scenario :

I have a array of promises, and for each they return resolve(object) if everything is good. but i have to check if my object has some property to be sure everything is good, and i have to do it after the call to my promise.

And i also need to know when every call will be done. I've thought about a int that increments every time each() is called and when my int reaches promisesArray.length - 1, then it's done, but i've seen that Bluebird as a method called all() that's called when everything is done.

Is it possible to mix them ? Like do an .each() on my array to check if everything return object has that property and then do an .all() to know everything went well ?

Answer Source

You can use Promise.all to get all promises, and then return Promise.resolve() or Promise.reject() if all promises include the property that you need for success:

Promise.all(promisesArray).then(function(results) {
    results.forEach(function(result) {
        // make the needed check for each result - for example:'property')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download