In pervious archive, we saw the Serial Trap: awaiting Task A, then awaiting Task B. This doubles the total wait time.
To fix this, we need Concurrency: initiating multiple operations at once and waiting for them as a group. JavaScript provides four static methods to handle this, each with different rules for success and failure.
[resultA, resultB, ...].Promise.all immediately rejects with that error. It ignores the others even if they are still running.const p1 = fetch("/user"); // Takes 1s
const p2 = fetch("/products"); // Takes 2s
async function loadPage() {
try {
// Both start NOW. Total time: 2s (not 3s)
const [user, products] = await Promise.all([p1, p2]);
console.log(user, products);
} catch (error) {
// If p1 fails, we land here immediately.
console.error("Critical failure:", error);
}
}const p1 = Promise.resolve("Success");
const p2 = Promise.reject("Network Error");
const results = await Promise.allSettled([p1, p2]);
console.log(results);
/* Output:
[
{ status: 'fulfilled', value: 'Success' },
{ status: 'rejected', reason: 'Network Error' }
]
*/const fetchData = fetch("/api/slow");
const timeout = new Promise((_, reject) =>
setTimeout(() => reject("Too slow!"), 3000)
);
try {
// If fetch takes 5s, timeout wins at 3s and throws error.
const data = await Promise.race([fetchData, timeout]);
console.log(data);
} catch (err) {
console.log("Request timed out:", err);
}const serverA = fetch("/us-east/data"); // Fails
const serverB = fetch("/eu-west/data"); // Succeeds in 1s
const serverC = fetch("/asia/data"); // Succeeds in 2s
const data = await Promise.any([serverA, serverB, serverC]);
console.log(data); // Returns result from serverB (first success)| Method | Wait Condition | Rejection Behavior | Use Case |
|---|---|---|---|
| Promise.all | Wait for ALL | Fail-Fast (Fails immediately if one fails) | Critical dependencies (all required). |
| Promise.allSettled | Wait for ALL | Never Fails (Returns status objects) | Bulk jobs, Analytics, Reporting. |
| Promise.race | First Settle | Wins/Fails based on the winner | Timeouts. |
| Promise.any | First Success | Fails only if ALL fail | Redundant APIs / Mirrors. |
Answer:
Promise.all rejects immediately, so our code moves to the catch block.
However, B is not cancelled. JavaScript promises generally cannot be cancelled once started. B will continue to run in the background (e.g., the network request will finish), but its result will be ignored by our code because we have already moved on.