Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Handle Multiple Promises with `Promise.allSettled()`

1 min read .
Handle Multiple Promises with `Promise.allSettled()`

Promise.allSettled() waits until every input promise has either fulfilled or rejected, then returns the outcome of each operation. Unlike Promise.all(), one rejection does not stop you from receiving the other results.

const tasks = [
  Promise.resolve('first result'),
  Promise.reject(new Error('second failed')),
  Promise.resolve('third result'),
];

const results = await Promise.allSettled(tasks);

for (const result of results) {
  if (result.status === 'fulfilled') {
    console.log('Value:', result.value);
  } else {
    console.error('Error:', result.reason);
  }
}

Each result has one of these shapes:

{ status: 'fulfilled', value: ... }
{ status: 'rejected', reason: ... }

When to Use It

Promise.allSettled() is useful when operations are independent and you need a complete report, such as sending notifications to several providers, processing multiple files, or collecting data from optional sources.

Use Promise.all() instead when the overall operation should fail as soon as any required promise rejects.

HTTP Requests Need Status Checks

A fulfilled fetch() promise can still represent an HTTP error such as 404 or 500. Check response.ok inside each task when HTTP status matters.

Conclusion

Use Promise.allSettled() when every outcome matters, even if some tasks fail. It gives you explicit fulfilled/rejected results without forcing the entire batch into an all-or-nothing model.

Related Posts

chevron-up