32. ASYNC | Static Promise Methods - In depth
This article will talk about static promise methods in depth.
Promise.all()
What it does?
Waits for all promises to resolve (fulfill) and returns the array of their results. If any one fails (reject), it becomes the error and all other results are ignored.
It takes an array (or iterable) of promises and returns a new Promise.
const newPromise = Promise.all(iterableOfPromises);
newPromise
.then()
.catch()
Promise.all() = "Run everything together, but give me the result only if everything succeeds."Core behavior
1. Runs all promises in parallel
const p1 = fetch('/api/user');
const p2 = fetch('/api/posts');
const p3 = fetch('/api/comments');
Promise.all([p1, p2, p3])
.then(([user, posts, comments]) => {
console.log(user, posts, comments);
});
π All 3 requests start at the same time, not one after another.
2. Resolves only when ALL succeed
The returned promise:
waits for every promise to resolve.
then gives results as an array (in order).
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
])
.then(result => {
console.log(result);
});
// Output: [1, 2, 3]
π Order is preserved, not based on completion time.
3. Fails fast (very important)
If any one promise rejects, the whole thing fails immediately.
Promise.all([
Promise.resolve(1),
Promise.resolve(3),
Promise.reject("Error!")
])
.catch(err => {
console.log(err);
});
// Output: "Error!"
π As soon as one fails: Promise.all() rejects, remaining results are ignored.
Real-world use cases
When you are dealing with multiple independent asynchronous operations together.
Fetching multiple APIs together.
Uploading multiple files.
Processing multiple images.
Running parallel DB queries.
When NOT to use Promise.all()
Avoid it when:
Tasks depend on each other.
You need partial results even if some fail.
You want to handle failures individually.
Important edge cases
1. Non-promises are allowed
Promise.all([1, 2, Promise.resolve(3)])
.then(console.log);
// Output: [1, 2, 3]
π Non-promises are treated as resolved values.
2. Empty array
Promise.all([]).then(console.log); // Output: []
π Resolves immediately.
π₯ Interview-level insight
The real power of Promise.all() is:
Parallelization β performance improvement
Synchronization β clean code structure
Promise.allSettled()
What it does?
Waits for all the promises to settle and returns their results as an array of objects with status and value.
It takes an array (or iterable) of promises and returns a new Promise.
const newPromise = Promise.allSettled(iterableOfPromises);
newPromise
.then()
.catch()
Promise.allSettled() = "Run everything, and tell me what happened to each one."Core behavior
1. Runs all promises in parallel
const p1 = fetch('/api/user');
const p2 = fetch('/api/posts');
const p3 = fetch('/api/comments');
Promise.all([p1, p2, p3])
.then(([user, posts, comments]) => {
console.log(user, posts, comments);
});
π All 3 requests start at the same time, not one after another.
2. Waits for ALL promises to settle
It waits until all promises get settle (resolve OR reject).
3. Returns structured results
Instead of raw values, we get array of objects describing each outcome:
Promise.allSettled([
Promise.resolve(1),
Promise.reject("Error!"),
Promise.resolve(3)
])
.then((results) => {
console.log(results);
});
/* Output:
[
{ status: "fulfilled", value: 1 },
{ status: "rejected", reason: "Error!" },
{ status: "fulfilled", value: 3 }
]
*/
Real-world use cases
When partial success is acceptable.
Bulk operations (π Some may fail, but you still want full report)
Sending emails to many users
Processing batch jobs
Uploading multiple files
Analytics / logging systems (π Some may fail, but you still want full report)
When NOT to use Promise.allSettled()
Avoid Promise.allSettled() when:
You need to stop immediately on failure.
Later operations depend on earlier success.
You donβt want to manually check statuses.
Important edge cases
1. Never rejects (almost always)
It always resolves, even if all promises fail. So, no need for .catch() (usually).
2. Order is preserved
Result order = input order (not completion order)
3. Non-promises are allowed
Promise.allSettled([1, Promise.reject("err")])
/* Output:
[
{ status: "fulfilled", value: 1 },
{ status: "rejected", reason: "err" }
]
*/
π₯ Interview-level insight
The real power of Promise.allSettled() is:
Resilience β system doesnβt break on partial failure
Observability β you get full insight into outcomes
Promise.race()
What it does?
Waits for the first promise to settle and its settled state (fulfill or reject) becomes the outcome.
It takes an array (or iterable) of promises and returns a new Promise.
const newPromise = Promise.race(iterableOfPromises);
newPromise
.then()
.catch()
Promise.race() = "Whichever finishes first, I go with that."Core behavior
1. First promise to settle (fulfilled or rejected) wins
// fulfilled
Promise.race([
new Promise(res => setTimeout(() => res("A"), 1000)),
new Promise(res => setTimeout(() => res("B"), 500))
])
.then(result => {
console.log(result);
});
// Output: "B"
// rejected
Promise.race([
new Promise((_, rej) => setTimeout(() => rej("Error"), 500)),
new Promise(res => setTimeout(() => res("Success"), 1000))
])
.catch(err => {
console.log(err);
});
// Output: "Error"
Real-world use cases
Request timeout mechanism.
Fetching from the fastest server: When data is cached in multiple locations (e.g., different CDN nodes),
Promise.race()selects the fastest source, reducing latency.Abandoning slow operations.
Cancel-like behavior / Short-circuiting Promises.
Important edge cases
1. Empty array
Promise.race([]);
π Returns a Promise that never settles (β οΈ dangerous).
2. Non-promises
Promise.race([42, Promise.resolve(10)])
.then(console.log); // Output: 42
π Non-promises are treated as already resolved. So, 42 wins instantly.
3. Other promises are NOT cancelled
Promise.race([p1, p2]);
π Even after one finishes, others continue running in background.
π₯ Interview-level insight
Promise.race()is about competition.Promise.race()is time-sensitive, not result-sensitive. It returns fastest success OR fastest failure.
Promise.any()
What it does?
Waits for the first promise to fulfill (not reject) and its result becomes the outcome. Throws
AggregateErrorif all the promises are rejected.It takes an array (or iterable) of promises and returns a new Promise.
const newPromise = Promise.any(iterableOfPromises);
newPromise
.then()
.catch()
Promise.any() = "Give me the first successful result, I donβt care about failures."Core behavior
1. First successful promise wins
Promise.any([
Promise.reject("Error 1"),
Promise.resolve("Success"),
Promise.resolve("Another success")
])
.then(result => {
console.log(result);
});
// Output: "Success"
π It ignores rejected promises.
π Resolves as soon as the first fulfilled promise appears (short-circuiting).
2. Only fails if ALL fail
Promise.any([
Promise.reject("Error 1"),
Promise.reject("Error 2")
])
.catch(err => {
console.log(err.name); // AggregateError
console.log(err.errors); // ["Error 1", "Error 2"]
});
π This is the only case where it rejects.
π It doesnβt throw a normal error. It throws special error called AggregateError.
Real-world use cases
Best for scenarios where you need the first successful result from a set of tasks, ignoring failures, making it ideal for high-availability systems. Common use cases include:
Trying multiple CDN mirrors.
Querying multiple database read-replicas.
Fetching data from redundant API services.
Important edge cases
1. Empty array
Promise.any([])
π Immediately rejects with AggregateError.
2. Non-promises
Promise.any([42, Promise.reject("err")])
.then(console.log); // Output: 42
π Non-promises are treated as already fulfilled.
3. Other promises keep running
Just like race():
π No cancellation.
π Others continue in background.
π₯ Interview-level insight
The real power of Promise.any() is:
Resilience β system doesnβt break on partial failure
Availability β any one service's availability is enough
Comparison
| Method | Success condition | Failure condition |
|---|---|---|
Promise.all() |
All succeed | Any fails |
Promise.allSettled() |
Always succeeds | Never fails |
Promise.race() |
First succeeds | First fails |
Promise.any() |
First succeeds | All fail |
Happy learning!

