Course contentsShow
← JavaScript
Lesson 1308 of 2,72329. PromisesFree lesson

Error Handling Strategies with Promise.all

Learn techniques to handle partial failures when using Promise.all, including wrapping individual promises.

Error Handling Strategies with Promise.all

You'll learn how to gracefully handle failures when running multiple promises in parallel without losing all your data.

The All-or-Nothing Problem

As you learned in lesson 1301, Promise.all rejects immediately if any promise fails. This "fail fast" behavior is great for operations where partial success is meaningless, but sometimes you want to capture whatever succeeded even if some operations failed.

Strategy 1: Wrap Individual Promises

The most common technique is wrapping each promise so it never rejects. Instead, it always resolves with either the value or the error:

function wrapPromise(promise) {
  return promise
    .then(value => ({ status: 'success', value }))
    .catch(error => ({ status: 'error', error }));
}

const promises = [
  fetch('/api/users'),
  fetch('/api/posts'),
  fetch('/api/invalid')  // This will fail
];

const wrapped = promises.map(wrapPromise);

Promise.all(wrapped).then(results => {
  results.forEach(result => {
    if (result.status === 'success') {
      console.log('Got data:', result.value);
    } else {
      console.log('Failed:', result.error);
    }
  });
});

Why This Works

By wrapping, every promise in the array always resolves successfully—either with a success object or an error object. Promise.all never sees a rejection, so it waits for everything to complete.

When to Use This

Use wrapped promises when:

  • Partial results are valuable
  • You want to log which operations failed
  • Some operations are optional or can be retried later

Key Takeaway: Wrapping individual promises in .then().catch() converts rejections into resolved values, allowing Promise.all to collect both successes and failures without short-circuiting.