Promise.all and Promise.race in JavaScript

Javascript 10 min min read Updated: Mar 09, 2026 Advanced
Promise.all and Promise.race in JavaScript
Advanced Topic 9 of 15

When working with asynchronous operations, developers often need to handle multiple promises at the same time. JavaScript provides built-in methods such as Promise.all() and Promise.race() to manage multiple promises efficiently.

These methods allow developers to run several asynchronous tasks simultaneously and control how their results are handled.

Promise.all()

The Promise.all() method takes an array of promises and waits for all of them to complete successfully. If all promises are fulfilled, it returns their results as an array.

Key Point: Promise.all() resolves only when all promises complete successfully.

Example of Promise.all()

javascript let promise1 = Promise.resolve("Task 1 completed"); let promise2 = Promise.resolve("Task 2 completed"); let promise3 = Promise.resolve("Task 3 completed"); Promise.all([promise1, promise2, promise3]) .then(function(results){ console.log(results); });
Output

[ "Task 1 completed", "Task 2 completed", "Task 3 completed" ]

In this example, Promise.all() waits until all three promises finish and then returns their results in an array.

Handling Errors in Promise.all()

If any promise fails, Promise.all() immediately rejects and returns the error.

javascript let promise1 = Promise.resolve("Success"); let promise2 = Promise.reject("Error occurred"); Promise.all([promise1, promise2]) .catch(function(error){ console.log(error); });
Output

Error occurred

Key Point: If one promise fails, Promise.all() stops and returns the error.

Promise.race()

The Promise.race() method runs multiple promises simultaneously but returns the result of the promise that completes first.

This means the fastest promise determines the final result.

Example of Promise.race()

javascript let promise1 = new Promise(function(resolve){ setTimeout(function(){ resolve("Task 1 finished"); },2000); }); let promise2 = new Promise(function(resolve){ setTimeout(function(){ resolve("Task 2 finished"); },1000); }); Promise.race([promise1, promise2]) .then(function(result){ console.log(result); });
Output

Task 2 finished

Since the second promise finishes faster, its result is returned.

Difference Between Promise.all() and Promise.race()

  • Promise.all() waits for all promises to complete.
  • Promise.race() returns the result of the first completed promise.
  • Promise.all() returns an array of results.
  • Promise.race() returns only the fastest result.
Key Point: Use Promise.all() when all tasks must complete, and use Promise.race() when only the fastest result matters.

Conclusion

Promise.all() and Promise.race() are powerful tools for managing multiple asynchronous operations in JavaScript. Promise.all() waits for all tasks to finish, while Promise.race() returns the result of the fastest task.

Understanding these methods helps developers efficiently manage parallel asynchronous tasks in modern JavaScript applications.

In the next tutorial, you will learn about JavaScript Fetch API, which allows developers to retrieve data from servers using HTTP requests.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators