Javascript Bluebird Promise.all - 多个承诺完成聚合成功和拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25817366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:55:08  来源:igfitidea点击:

Bluebird Promise.all - multiple promises completed aggregating success and rejections

javascriptnode.jspromisebluebird

提问by j03m

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:

今天有人用 bluebird 提出了一个有趣的案例,处理多个承诺的最佳方法是什么,我们对在给定的履行或拒绝上停止不感兴趣,而是对检查最终结果感兴趣。一个例子:

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.

在这里,如果我们运行 map 或所有被拒绝的承诺将导致处理程序不报告结果。

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

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

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

Process finished with exit code 0

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

这里执行了每个 promise 的代码,但只报告了 1 个结果和 1 个错误。该错误导致进程停止。

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

如果我们取消注释 .all 我们会得到类似的行为。这次只报了错误。任何成功都不会进入那时(可以理解)。

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?

鉴于这种行为,实现一个场景的最佳方法是什么,在该场景中运行所有承诺,并报告履行承诺的结果,并报告任何和所有拒绝?

Something like:

就像是:

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

回答by Benjamin Gruenbaum

You'd use .reflect:

你会使用.reflect

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

This used to be handled with a settlefunction that did this for an array traditionally - it was generalized by .reflectsince it separates aggregation from the notion of a promise inspection and lets you do what .settledid but to other actions like .anyor .someas well.

这曾经是通过一个settle传统上为数组执行此操作的函数来处理的- 它被概括为.reflect因为它将聚合与承诺检查的概念分开,并允许您执行.settle除其他操作之外的其他操作,例如.any.some