Avraam Mavridis Avraam Mavridis - 3 years ago 73
Javascript Question

Keep the values only from the Promises that resolve and ignore the rejected

I have an array of promises, each promise is a request to scrap a website. Most of them resolve but may are cases that one or two reject e.g. the website is down. What I want is to ignore the rejected promises and keep the values only of the promises that have been resolved.

is not for that case since it requires all the promises to resolve.

is not what I want since I don't know beforehand how many promises will resolve.

is the same as
with count 1.

How can this case being solved? I am using the Bluebird implementation.

Answer Source

Sure thing, lucky for you bluebird already does this:

    for(var i = 0; i < results.length; i++){
           // results[i].value() to get the value

You can also use the newer reflect call:

    return el.reflect(); 
    return p.isFulfilled();
    // only fulfilled promises here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download