Promise.all() / allSettled() / race() / any() Since: ES6(ECMAScript 2015)
A set of static methods for managing multiple Promises together. Four methods are available depending on your needs — waiting for all to complete, waiting for the first to complete, and more.
Syntax
// Waits for all Promises to succeed. Fails immediately if any one of them fails. Promise.all([promise1, promise2, ...]); // Waits for all Promises to settle, regardless of success or failure. Promise.allSettled([promise1, promise2, ...]); // Returns the result of the first Promise to settle, regardless of success or failure. Promise.race([promise1, promise2, ...]); // Returns the result of the first Promise to succeed. Fails only if all of them fail. Promise.any([promise1, promise2, ...]);
Method List
| Method | Description |
|---|---|
| Promise.all(array) | Returns an array of results if all Promises succeed. If any one fails, it immediately rejects with the first error that occurred. |
| Promise.allSettled(array) | Waits for all Promises to settle and returns an array of each result, including both fulfilled and rejected outcomes. |
| Promise.race(array) | Returns the result of the first Promise to settle. The outcome — success or failure — is determined by whichever resolves or rejects first. |
| Promise.any(array) | Returns the result of the first Promise to succeed. Rejects with an AggregateError only if all Promises fail. |
Sample Code
// Helper function that creates a test Promise
function fetchData(name, ms, shouldFail) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (shouldFail) {
reject(name + " fetch failed");
} else {
resolve(name + " data");
}
}, ms);
});
}
// Promise.all() waits for all Promises to succeed.
Promise.all([
fetchData("User", 1000, false),
fetchData("Post", 2000, false),
fetchData("Comment", 1500, false)
]).then(function(results) {
console.log(results); // If all succeed, an array of results is logged.
}).catch(function(error) {
console.log(error); // If any one fails, this runs instead.
});
// Promise.allSettled() collects all results regardless of success or failure.
Promise.allSettled([
fetchData("API-A", 1000, false),
fetchData("API-B", 500, true),
fetchData("API-C", 1500, false)
]).then(function(results) {
results.forEach(function(r) {
if (r.status === "fulfilled") {
console.log("Success: " + r.value);
} else {
console.log("Failure: " + r.reason);
}
});
});
// Promise.race() returns the result of the first Promise to settle.
Promise.race([
fetchData("Fast server", 500, false),
fetchData("Slow server", 3000, false)
]).then(function(result) {
console.log(result); // The result of whichever settles first is logged.
});
// Promise.any() returns the result of the first Promise to succeed.
Promise.any([
fetchData("Server 1", 1000, true),
fetchData("Server 2", 2000, false),
fetchData("Server 3", 1500, false)
]).then(function(result) {
console.log(result); // The result of the first successful Promise is logged.
});
Overview
These four methods are designed to efficiently manage multiple asynchronous operations. The most commonly used is Promise.all(), which excels at fetching data from multiple APIs simultaneously and processing all the results together once every request has completed. Because the Promises run in parallel, the total time is shorter than running them sequentially.
Because Promise.all() fails as a whole if even one Promise rejects, use Promise.allSettled() when you want to tolerate partial failures. Each result includes a status property: "fulfilled" means success and the result is in value, while "rejected" means failure and the error is in reason.
Promise.race() is often used to implement timeouts — you race a data-fetching Promise against a timer Promise so that if no response arrives within a set time, the timeout wins. Promise.any() is well-suited for redundancy patterns where you send requests to multiple servers simultaneously and use whichever responds successfully first.
Browser Compatibility
31 or earlier ×
28 or earlier ×
7 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
37+ ○
31 or earlier ×
Firefox Android
79+ ○
28 or earlier ×If you find any errors or copyright issues, please contact us.