最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep the values only from the Promises that resolve and ignore the rejected - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited May 18, 2015 at 17:24 Avraam Mavridis asked May 18, 2015 at 17:18 Avraam MavridisAvraam Mavridis 8,92022 gold badges86 silver badges135 bronze badges 2
  • if you could add a timeout to get, they should all eventually "resolve". So I am wondering if you wanted to periodically test and see how many have resolved? – Dinesh Commented May 18, 2015 at 17:26
  • @Dinesh well, thats not a reliable solution, what if it doesnt, if the resource is down for hours? There should be a more reliable solution. – Avraam Mavridis Commented May 18, 2015 at 17:30
Add a ment  | 

2 Answers 2

Reset to default 15

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
});

this is same as @Benjamin solution but with more elegant array manipulation:

this code will ignore the rejected promise result, so you may run 10 promises and have the results as array of 3 elements:

Promise.settle(arrayOfPromises).then(function(results){
    return results.filter(function (result) {
               return result.isFulfilled();
            }).map(function (result) {
               return result.value();
            });   
}).then(function(results){
     console.log("here you go:", results);
});

here will ignore the rejected promise but will place null as result so if you run 10 promises you will have the results as array of 10 elements the rejected value will be null:

Promise.settle(arrayOfPromises).then(function(results){
    return results.map(function (result) {
               return result.isFulfilled()? result.value() : null;
            }); 
}).then(function(results){
     console.log("here you go:", results);
});
发布评论

评论列表(0)

  1. 暂无评论