Understanding promise's methods is important to call APIs in parallel and it's an important concept to know for any machine coding interview.
Anuj Sharma
Last Updated Nov 10, 2025
Understanding promises static methods is very important for handling promises in different scenarios, especially in API calls. This concept is very important to understand for javascript and machine coding interview rounds
all fulfilled responses If anyone fails, the whole result fails with an error.all settled response (either fulfilled or rejected)first settled response (either fulfilled or rejected )first fulfilled response if all rejected return AggregateError object with all the rejected errors as part of the errors objectLet's cover the promise methods and their use-cases one by one to understand these promise methods using code implementations.
It returns an array of results in case all the promises got `resolved`, in case of any failure it returns the error immediately and other promises are ignored.
Promise.all([promises]) used to call promises in parallel.
Here are the different use cases with examples:
let p = Promise.all([10, Promise.resolve(30), 40]);
p.then((values) => console.log(values)) // log: [10, 30, 40]
// Synchronously return resolved promise
let p = Promise.all([])
p.then((value) => console.log(value)) // log: []
// Asynchronously return resolved promise in case all non-promise iterators
let p = Promise.all([10, 20, 30])
p.then((value) => console.log(value)) // log: [10, 20, 30]
let p = Promise.all([ Promise.resolve(44), 55, 66])
p.then((value) => console.log(value)) // log: [44, 55, 66]
// Return first rejected promise if there is any rejection
let p = Promise.all([
Promise.resolve(10),
Promise.reject('first reject'),
Promise.reject('second reject')
])
p.then(null, (err) => {
console.log(err) // Log: 'first reject'
})
// or
p.catch((err) => {
console.log(err) // Log: 'first reject'
})
Promise.allSettled just waits for all promises to settle, regardless of the result. The resulting array has
{ status: "fulfilled", value: result } for successful responses{status: "rejected", reason: error} for errors.Example:
let urls = [
'https://api.github.com/users/iliakan',
'https://api.github.com/users/remy',
'https://no-such-url'
];
Promise.allSettled(urls.map( async url => await fetch(url)))
.then(results => {
results.forEach((result, num) => {
// Fulfilled results
if (result.status == "fulfilled") {
alert(`${urls[num]}: ${result.value.status}`);
}
// Rejected results
if (result.status == "rejected") {
alert(`${urls[num]}: ${result.reason}`);
}
});
});
It works similarly Promise.all but waits only for the first settled promise and gets its result (or error).
Example:
// First fulfilled
Promise.race([
new Promise((resolve, reject) => setTimeout(() => resolve(1), 1000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")),2000)),
new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000))
]).then(alert); // 1
// First rejected
Promise.race([
new Promise((resolve, reject) => setTimeout(() => resolve(1), 5000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")), 2000)),
new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000))
]).then(alert)
.catch(alert) // Whoops!
Similar to Promise.race, but waits only for the first fulfilled promise and gets its result.
If all of the given promises are rejected, then the returned promise is rejected with AggregateError – a special error object that stores all promise errors in its errors property.
Case 1: Where the first promise got rejected, and the second promise got fulfilled successfully so the code returns the first fulfilled promise. It won't go to the 3rd promise.
Promise.any([
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")),1000)),
new Promise((resolve, reject) => setTimeout(() => resolve(1), 2000)), // 1st fulfilled
new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000))
]).then(alert); // 1
Case 2: In the case where all the promises got rejected and there is no fulfilled promise, Code returns a custom object AggregateError which contains error messages for all the rejected promises.
Promise.any([
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Ouch!")), 1000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Error!")), 2000))
]).catch(error => {
console.log(error.constructor.name); // AggregateError
console.log(error.errors[0]); // Error: Ouch!
console.log(error.errors[1]); // Error: Error!
});
The use of Promise Static methods is very common in real-world frontend development while fetching data using REST API calls. Now that you have understood the workings of Promise Static methods (Promise.all, Promise.allSettled, Promise.race, Promise.any), you can easily manage the success and failure scenarios very efficiently while calling APIs.
Anuj Sharma
Last Updated Nov 10, 2025
Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Nov 10, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Anuj Sharma
Last Updated Nov 8, 2025
In this post, we will going to cover the step-by-step implementation of Infinite Currying Sum with a code example. This is one of the most common JavaScript Interview questions.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved