Open In App

JavaScript - How to Handle Errors in Promise.all?

Last Updated : 25 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To handle errors in Promise.all(), you need to understand that it fails immediately if any of the promises reject, discarding all resolved values. This behavior can lead to incomplete operations and potential application crashes.

The best way to manage errors in Promise.all() is by using .catch() inside each promise, switching to Promise.allSettled() to capture both resolved and rejected results, or wrapping Promise.all() inside a try...catch block when using async/await.

Handling Errors in Promise.all()

1. Using Promise.allSettled()

If you want all promises to run without failures, use Promise.allSettled(), which returns an array of results including both fulfilled and rejected promises.

JavaScript
const promises = [
    Promise.resolve("Promise 1 Success"),
    Promise.reject("Promise 2 Failed"),
    Promise.resolve("Promise 3 Success")
];

Promise.allSettled(promises)
    .then(results => console.log("All Settled Results:", results));

Output
All Settled Results: [
  { status: 'fulfilled', value: 'Promise 1 Success' },
  { status: 'rejected', reason: 'Promise 2 Failed' },
  { status: 'fulfilled', value: 'Promise 3 Success' }
]

In this example

  • Each promise result includes { status: "fulfilled", value: result } for success or { status: "rejected", reason: error } for failure.
  • Unlike Promise.all(), this method does not discard fulfilled results when one promise fails, making error handling more efficient.

2. Using async/await

The async/await syntax streamlines asynchronous operations, allowing code to pause and resume tasks, enhancing readability and responsiveness.

JavaScript
async function executePromises() {
    try {
        const results = await Promise.all([
            Promise.resolve("Promise 1"),
            Promise.reject("Promise 2 Error"),
            Promise.resolve("Promise 3")
        ]);
        console.log("Results:", results);
    } catch (error) {
        console.error("Caught Error:", error);
    }
}

executePromises();

Output

Caught Error: Promise 2 Error

This method catches the first rejection and prevents the application from crashing.

3. Manually Handling Errors for Each Promise

An alternative approach is to catch individual errors within each promise, ensuring one failure doesn't break the entire chain.

JavaScript
const safePromise = (promise) => {
    return promise.catch(error => ({ error }));
};

Promise.all([
    safePromise(Promise.resolve("Promise 1 Success")),
    safePromise(Promise.reject("Promise 2 Failed")),
    safePromise(Promise.resolve("Promise 3 Success"))
]).then(results => console.log("Results:", results));

Output
Results: [
  'Promise 1 Success',
  { error: 'Promise 2 Failed' },
  'Promise 3 Success'
]

In this example

  • safePromise ensures that if a promise fails, it doesn't reject Promise.all() but instead returns { error: "Error Message" }.
  • Even if one promise fails, the remaining promises continue execution.
  • The output contains both resolved values and errors in a structured format.
  • This method prevents unexpected crashes and allows handling partial results.

Best Practices for Handling Errors in Promise.all()

  • Use Promise.allSettled() if all promises should run, even if some fail.
  • Wrap each promise with .catch() to prevent failures from breaking the entire chain.
  • Use try...catch with async/await to handle errors in asynchronous functions.
  • Log and analyze errors properly to prevent silent failures in production.
  • Consider retry mechanisms for network requests and external API calls.

Similar Reads