Promise.race and Promise.all are two methods used with Promises in JavaScript, but they have different purposes and behaviors:
Promise.race:
Promise.race is used when you have an array of Promises, and you want to resolve or reject as soon as the first Promise in the array resolves or rejects.
It returns a new Promise that resolves or rejects with the result of the first Promise in the array to settle (either resolve or reject). The order in which Promises settle does not matter; it depends on which Promise resolves or rejects first.
If any of the Promises in the array settles (either resolves or rejects), the resulting Promise settles immediately with the same result.
Promise.race is often used for scenarios where you want to implement a timeout for an asynchronous operation. For example, you can race a Promise that represents the asynchronous operation with a timer Promise, and if the timer Promise resolves first, you can consider it a timeout.
const promise1 = new Promise((resolve) => setTimeout(() => resolve("Promise 1"), 1000));
const promise2 = new Promise((resolve) => setTimeout(() => resolve("Promise 2"), 500));
Promise.race([promise1, promise2])
.then((result) => {
console.log("Result:", result); // Resolves with "Promise 2" because it settles first
});
Promise.all:
Promise.all is used when you have an array of Promises, and you want to wait for all of them to resolve successfully. It returns a new Promise that resolves with an array of results when all Promises in the input array have resolved successfully.
If any of the Promises in the array reject (encounter an error), the resulting Promise will immediately reject with the reason of the first Promise that rejects.
Promise.all is often used in scenarios where you have multiple asynchronous operations that need to complete successfully before proceeding with further processing, such as fetching data from multiple sources and combining the results.
Example of Promise.all:
const promise1 = new Promise((resolve) => setTimeout(() => resolve("Result 1"), 1000));
const promise2 = new Promise((resolve) => setTimeout(() => resolve("Result 2"), 500));
Promise.all([promise1, promise2])
.then((results) => {
console.log("Results:", results); // Resolves with ["Result 1", "Result 2"] when both Promises are resolved
});
In summary, Promise.race resolves with the result of the first settled Promise (either resolved or rejected), while
Promise.all resolves with an array of results when all Promises in the array have successfully resolved. They serve different use cases based on the specific asynchronous requirements of your code.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Promise.race and Promise.all are two methods used with Promises in JavaScript, but they have different purposes and behaviors:
Promise.race:
Promise.all:
Example of Promise.all:
In summary, Promise.race resolves with the result of the first settled Promise (either resolved or rejected), while Promise.all resolves with an array of results when all Promises in the array have successfully resolved. They serve different use cases based on the specific asynchronous requirements of your code.