Bluebird Promise.all

编程入门 行业动态 更新时间:2024-10-27 21:12:37
本文介绍了Bluebird Promise.all - 多个承诺完成聚合成功和拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

推荐答案

你会使用 .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()
     }
  });
});

这曾经是用 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.

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

更多推荐

[db:关键词]

本文发布于:2023-03-30 02:47:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/768614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Bluebird   Promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!