둘 이상의 비동기 작업을 기다린 뒤 처리해야할 경우 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로 빠진다.
'자바스크립트 > 비동기' 카테고리의 다른 글
promise를 동기적인 모양으로 처리하는 async와 await (0) | 2020.07.04 |
---|---|
axios로 서버에 요청할 때 생기는 에러 처리 방법 (2) | 2020.06.30 |
자바스크립트의 try, catch (0) | 2020.04.30 |
async 함수와 await에 관한 팁 (0) | 2020.04.29 |
Promise 이해하기 (0) | 2020.04.28 |