Promise.allSettled()
연결된 노트와 함께 읽기
예시
JavaScript
const promise = Promise.allSettled([getName(), getTodo()]);
promise.then((data) => {
console.log(data);
})
function getName() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('철수');
// reject(new Error('에러: 이름이 없어요'));
}, 1000)
})
}
function getTodo() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('밥먹기');
// reject(new Error('에러: 할일이 없어요));
}, 2000)
})
}
// 실행결과
(2) [{…}, {…}]
0:
reason: Error: 에러: 이름이 없어요 // 만약 실패했다고 가정
status: "rejected"
[**prototype**](/vault/prototype): Object
1:
status: "fulfilled"
value: "밥먹기"
[**prototype**](/vault/prototype): Object
length: 2
[**prototype**](/vault/prototype): Array(0)