承诺-在Promise.all中捕获所有拒绝 [英] Promises - catching all rejections in a Promise.all

查看:141
本文介绍了承诺-在Promise.all中捕获所有拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个伪代码

var Promise = require('bluebird')
function rej1(){
    return new Promise.reject(new Error('rej1'));
}

function rej2() {
    return new Promise.reject(new Error('rej2'));
}
function rej3() {
    return new Promise.reject(new Error('rej3'));
}

Promise.all([rej1(),rej2(),rej3()] ).then(function(){
    console.log('haha')
},function(e){
    console.error(e);
})

在rejectHandler中,我仅看到第一个拒绝.是否可以查看所有三个拒绝?

In the rejectionHandler i see only the first rejection. Is it possible to view all three rejections?

推荐答案

是的,可以查看所有三个拒绝项.一旦一个承诺被拒绝,Promise.all就被拒绝.相反-使用 Promise.settle :

Yes, it is possible to view all three rejections. Promise.all rejects as soon as one promise rejects. Instead - use Promise.settle:

Promise.settle([rej1(), rej2(), rej3()).then(function(results){
    var rejections = results.filter(function(el){ return el.isRejected(); });
    // access rejections here
    rejections[0].reason(); // contains the first rejection reason
});

这篇关于承诺-在Promise.all中捕获所有拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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