약속 - 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);
})
RejectionHandler에는 첫 번째 거부만 표시됩니다.거부된 세 가지 항목을 모두 볼 수 있나요?
해결책
예, 세 가지 거부 사항을 모두 볼 수 있습니다. 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