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.
Promise.all
is not for that case since it requires all the promises to resolve.
Promise.some()
is not what I want since I don't know beforehand how many promises will resolve.
Promise.any()
is the same as Promise.some()
with count 1.
How can this case being solved? I am using the Bluebird implementation.
Sure thing, lucky for you bluebird already does this:
Promise.settle(arrayOfPromises).then(function(results){
for(var i = 0; i < results.length; i++){
if(results[i].isFulfilled()){
// results[i].value() to get the value
}
}
});
You can also use the newer reflect
call:
Promise.all(arrayOfPromises.map(function(el){
return el.reflect();
})).filter(function(p){
return p.isFulfilled();
}).then(function(results){
// only fulfilled promises here
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With