Syncing account progress…

Back

Intermediate · 12 min

Work with parallel promises

Combine independent operations and distinguish all-or-nothing results from per-item outcomes.

Combine independent results

Promise.all() fulfills with results in input order when all inputs fulfill. It rejects if an input rejects. It does not cancel the other operations.

const results = await Promise.all([Promise.resolve("Profile"), Promise.resolve("Settings")]);
console.log(results.join(", "));

Output

Profile, Settings

Inspect every outcome

Promise.allSettled() waits for every input and reports each status. Use it when one failed item should not hide the others.

const results = await Promise.allSettled([Promise.resolve(3), Promise.reject(new Error("Offline"))]);
console.log(results.map(result => result.status).join(", "));

Output

fulfilled, rejected

Put it into practice

  1. Predict each result before running the example.
  2. Complete the coding task, then explain why your solution works.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Combine two totals

Await both operations with Promise.all(), then add their results and display total.

Practice