Asynchronous JavaScript

Wait for all promises to resolve in JavaScript

Learn how to start multiple promises at once and wait for all of them to resolve using await Promise.all(), instead of awaiting each one after another.

8 minute lesson

~~~

Sequential awaits start the second operation only after the first finishes:

const values = await store.getAll()
const keys = await store.getAllKeys()

If the operations are independent, start them together:

const [values, keys] = await Promise.all([
  store.getAll(),
  store.getAllKeys()
])

The result order matches the input order, not completion order. Total waiting time is roughly the slowest operation rather than the sum of both.

Promise.all() rejects as soon as one input rejects. Other operations are not automatically cancelled; they may continue doing network or database work. Use AbortController where the underlying API supports cancellation, and design side effects so a partial completion is safe.

Use sequential awaits when the second operation depends on the first, when ordering is required, or when starting everything together would overwhelm a service. For a large list, limit concurrency instead of creating thousands of requests at once.

When every outcome matters, use Promise.allSettled() and inspect each { status, value } or { status, reason } result. Do not switch to it merely to suppress errors; decide how partial failure should appear to the user.

Wrap the group in try/catch and attach context to the failure:

try {
  const [profile, orders] = await Promise.all([getProfile(), getOrders()])
  renderAccount(profile, orders)
} catch (error) {
  showError('The account could not be loaded')
}

If you want to see how Promise.all() behaves compared to the other combinators, try the promise combinators tool.

Try it: create two timed promises, compare sequential and concurrent duration, then reject one and observe what happens to the other.

Lesson completed