CodeBlanks Logo Icon CodeBlanks

Promise.allSettled

JavaScript course · Advanced · Lesson 72/100

Wait for all promises to settle and learn each one's outcome.

Promise.allSettled

Promise.allSettled waits until every promise it receives has settled. It never gives up when one rejects; it still completes the rest. The result is an array of objects, one per promise: { status: 'fulfilled', value } or { status: 'rejected', reason }.

const results = await Promise.allSettled([
  fetch('/users'),
  fetch('/products')
]);

When several API calls run and some fail, you do not lose the rest. You can inspect each result and keep the successful ones.

Filtering results

Each result object has a status property. When it is 'fulfilled' you read value; when it is 'rejected' you read reason. To pick the successful results, filter the array.

const ok = results.filter(r => r.status === 'fulfilled');
const values = ok.map(r => r.value);

This pattern is great for partial batch jobs: you can see exactly which calls passed and which failed.

Exercise Filter successful calls

Define three promises: p1 and p3 resolve with a value via Promise.resolve, p2 rejects via Promise.reject. Wait for all three with Promise.allSettled. Filter the results to keep only those with status === 'fulfilled' and log their values with console.log.

Show example solution
const p1 = Promise.resolve('user-1');
const p2 = Promise.reject('offline');
const p3 = Promise.resolve('user-3');
const results = await Promise.allSettled([p1, p2, p3]);
const ok = results.filter(r => r.status === 'fulfilled');
console.log(ok.map(r => r.value));

Test yourself

  1. What is the key difference between Promise.all and Promise.allSettled?

    • They behave exactly the same way
    • allSettled is always faster
    • allSettled includes rejections in the results
    • Promise.all returns every result
    Show answer

    allSettled includes rejections in the results — Correct! allSettled adds rejected promises to the results as { status: 'rejected' }.

  2. Check the result status

    const r = await Promise.allSettled([p1, p2]);
    if (r[0].____ === 'fulfilled') {
      console.log(r[0].value);
    }
    Show answer
    const r = await Promise.allSettled([p1, p2]);
    if (r[0].status === 'fulfilled') {
      console.log(r[0].value);
    }
  3. Match the Promise methods

    allSettled
    all settle
    all
    stops at first reject
    race
    first to settle
    any
    first to fulfil

All lessons in Advanced