Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promises - catching all rejections in a Promise.all [duplicate]

I have this dummy code

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

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

like image 330
user2468170 Avatar asked Jul 31 '14 10:07

user2468170


1 Answers

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
});
like image 100
Benjamin Gruenbaum Avatar answered Nov 03 '22 04:11

Benjamin Gruenbaum