【问题标题】:Promises - catching all rejections in a Promise.all [duplicate]Promises - 在 Promise.all 中捕获所有拒绝 [重复]
【发布时间】:2014-09-23 06:32:40
【问题描述】:

我有这个虚拟代码

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

在rejectionHandler 中,我只看到第一个拒绝。是否可以查看所有三个拒绝?

【问题讨论】:

    标签: node.js promise bluebird


    【解决方案1】:

    是的,可以查看所有三个拒绝。 Promise.all 在一个承诺被拒绝时立即拒绝。相反 - 使用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
    });
    

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 2020-05-25
      • 1970-01-01
      • 2021-04-21
      • 2018-03-24
      • 2015-10-06
      • 2021-08-13
      • 2019-03-10
      • 1970-01-01
      相关资源
      最近更新 更多