.all()은 여러 개의 Promise를 일괄적으로 처리할 때 사용합니다.
.all의 인자로는 Promise 객체의 배열 등을 넘겨줄 수 있습니다.
const promises = []
const asyncFunc = async (i) => {
const res = await fetch(`http://.../${i}`)
console.log(res)
}
for (let i=0; i<10; i++) {
// promises 배열에 Promise 객체를 추가합니다.
promises.push(asyncFunc(i))
}
// 10개의 Promise들이 모두 끝날 때 까지 대기합니다.
const result = await Promise.all(promises)
console.log(result)
Promise.all()은 배열의 모든 Promise가 fufiled 상태가 될 때 fulfiled 상태가 됩니다.
만약 하나 이상의 요소가 rejected 상태가 된다면, Promise.all() 역시 rejected가 됩니다.
728x90
'Language > JavaScript' 카테고리의 다른 글
| [JS] Library | 02. Papaparse (0) | 2024.12.10 |
|---|---|
| [JS] Library | 01. Superstruct (0) | 2024.08.25 |
| [JS] Syntax | 08. .then() (0) | 2024.07.27 |
| [JS] Syntax | 07. await & async (0) | 2024.07.25 |
| [JS] Syntax | 06. Promise (0) | 2024.07.24 |