[JS] Syntax | 09. Promise.all()

2024. 7. 27. 14:07·Language/JavaScript

.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
'Language/JavaScript' 카테고리의 다른 글
  • [JS] Library | 02. Papaparse
  • [JS] Library | 01. Superstruct
  • [JS] Syntax | 08. .then()
  • [JS] Syntax | 07. await & async
Rayi
Rayi
  • Rayi
    아카이브
    Rayi
  • 전체
    오늘
    어제
    • 분류 전체보기 (276)
      • CS (40)
        • CV (2)
        • PS (34)
      • Reveiw (18)
        • Paper (18)
        • Github (0)
      • ML (8)
        • Pytorch (5)
      • Language (59)
        • Python (8)
        • JavaScript (32)
        • TypeScript (16)
        • C++ (3)
      • IDE (12)
      • Git (13)
      • Frontend (77)
        • React (8)
        • ReactNative (6)
        • SolidJS (20)
        • CSS (12)
      • Backend (44)
        • DB (18)
        • Node.js (11)
      • UI (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    CV
    frontend
    backend
    API
    ps
    Git
    figma
    review
    Three
    DB
    GAN
    python
    deploy
    Express
    postgresql
    PRISMA
    vscode
    ML
    ReactNative
    react
    SOLID
    CS
    mongo
    PyTorch
    CSS
    expo
    js
    modal
    ts
    nodejs
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Rayi
[JS] Syntax | 09. Promise.all()
상단으로

티스토리툴바