자바스크립트/비동기
Promise.all
Socratone
2020. 7. 4. 09:11
둘 이상의 비동기 작업을 기다린 뒤 처리해야할 경우 Promise.all을 쓴다.
const p1 = new Promise((resolve, reject) => {
/* ... */
resolve(1);
});
const p2 = new Promise((resolve, reject) => {
/* ... */
resolve(2);
});
Promise.all([p1, p2]); // p1과 p2가 resolve 될 때 resolve된 promise를 리턴한다.
.then(result => console.log(result))
.catch(error => console.log(error)); // 둘 중 하나라도 reject 되면 catch로 빠진다.