蓝鸟Promise.all-完成了多个承诺,聚集了成功和拒绝 [英] Bluebird Promise.all - multiple promises completed aggregating success and rejections

查看:94
本文介绍了蓝鸟Promise.all-完成了多个承诺,聚集了成功和拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天有人与bluebird提出了一个有趣的案例,这是处理多重承诺的最佳方法,其中我们对停止履行既定的目标或拒绝不感兴趣,而对检查最终结果感兴趣.一个例子:

Someone brought up an interesting case today with bluebird, what is the best way to handle multiple promises where we're not interested in stopping on a given fulfillment or rejection but rather interested in inspecting the final result. An example:

var p1 = new Promise(function(f,r){
    setTimeout(function(){
        console.log("p1");
        f("yay");
    }, 100);

});

var p2 = new Promise(function(f,r){
    setTimeout(function(){
        console.log("p2");
        r(new Error("boo"));
    }, 200);

})

var p3 = new Promise(function(f,r){
    setTimeout(function(){
        console.log("p3");
        r(new Error("yay"));
    }, 300);

});

var p4 = new Promise(function(f,r){
    setTimeout(function(){
        console.log("p4");
        f("yay");
    }, 400);

});


//Promise.all([p1,p2, p3, p4]).then(function(p){
//    console.log("Results:",p);
//}).error(function(e){
//    console.log("Error:",e);
//});

Promise.map([p1,p2, p3, p4],function(p){
    console.log("results:",p);
}, {concurrency:10}).error(function(e){
    console.log("Error:",e);
});

在这里,如果我们运行地图,否则所有被拒绝的诺言将导致处理程序不报告结果.

Here, if we run either map or all the rejected promises will cause handlers not to report results.

例如,运行上述实现的Promise.map的结果是:

For example the results of running Promise.map as implemented above is:

debugger listening on port 65222
p1
results: yay
p2
Error: [Error: boo]
p3
p4

Process finished with exit code 0

在这里执行每个Promise的代码,但是只报告1个结果和1个错误.该错误导致进程停止.

Here the code for each promise executes, but only 1 result and 1 error is reported. The error causes the process to stop.

如果我们取消注释所有内容,我们将得到类似的行为.这次,仅报告错误.任何成功都没有做到(可以理解).

If we uncomment .all we get similar behavior. This time, only the error is reported. Any successes do not make it into then (understandably).

debugger listening on port 65313
p1
p2
Error: [Error: boo]
p3
p4

Process finished with exit code 0

鉴于这种行为,实现一个方案的最佳方法是实现所有承诺都得到执行,并且兑现承诺的结果并报告所有拒绝的情况?

Given this behavior what would be the best way to go about implementing a scenario where by all promises are run and the results of fulfilled promises are reported with any and all rejections?

类似的东西:

Promise.aggregate([p1,p2,p3,p4]).then(function(fulfilled, rejected){
    console.log(fulfilled); //all success
    console.log(rejected); //any and all rejections/exceptions
});

推荐答案

您将使用 :

Promise.all([p1,p2,p3,p4].map(x => x.reflect()).then(results => {
  results.forEach(result => {
     if(result.isFulfilled()){
         // access result.value()
     } else {
         // access result.reason()
     }
  });
});

过去通常使用settle函数处理该函数,该函数通常是对数组执行此操作的-它由.reflect进行了概括,因为它将聚合与承诺检查的概念分开了,并允许您执行.settle的操作,但是以及.any.some之类的其他动作.

This used to be handled with a settle function that did this for an array traditionally - it was generalized by .reflect since it separates aggregation from the notion of a promise inspection and lets you do what .settle did but to other actions like .any or .some as well.

这篇关于蓝鸟Promise.all-完成了多个承诺,聚集了成功和拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆