Promise - 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);
})
拒否ハンドラでは、最初の拒否のみが表示されます。3 つの拒否をすべて表示することはできますか?
解決
はい、3 つの拒否をすべて表示することができます。 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
});
所属していません StackOverflow