Syntax
Promise.all([p1, p2, p3])
Promise.allSettled([p1, p2, p3])Examples
Promise.all() - All or Nothing
Running independent requests in parallel and waiting for all of them.
const userPromise = fetch("/api/user").then(r => r.json());
const postsPromise = fetch("/api/posts").then(r => r.json());
Promise.all([userPromise, postsPromise])
.then(([user, posts]) => {
console.log(user, posts);
})
.catch(error => {
console.log("At least one request failed:", error.message);
});Promise.allSettled() - Getting Every Result
Useful when you need results from every promise, even if some fail.
const promises = [
Promise.resolve("Success 1"),
Promise.reject("Failure 1"),
Promise.resolve("Success 2")
];
Promise.allSettled(promises).then(results => {
results.forEach(result => {
if (result.status === "fulfilled") {
console.log("Success:", result.value);
} else {
console.log("Failed:", result.reason);
}
});
});Best practices
- Use Promise.all() when every operation must succeed for the result to be useful - like loading required page data
- Use Promise.allSettled() when partial success is acceptable and you want to know exactly what failed without stopping the rest
- Use Promise.race() for timeout patterns - race a real request against a promise that rejects after a delay
- Avoid awaiting promises sequentially in a loop when they are independent - use Promise.all() with map() to run them in parallel instead
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.Promises
A Promise represents a value that may not be available yet - the eventual result of an asynchronous operation. It exists in one of three states: pending, fulfilled, or rejected. then() handles a successful result, catch() handles an error, and finally() runs regardless of outcome. Promises are the foundation that async/await is built on top of.Fetch API
The Fetch API provides a modern, promise-based way to make HTTP requests, replacing the older XMLHttpRequest. fetch() returns a promise that resolves to a Response object once headers are received - note that a Response is only considered "ok" if the status is 2xx, so checking response.ok is essential, since fetch() does not reject on HTTP error statuses.Web Workers
Web Workers run JavaScript on a separate background thread, away from the main UI thread, so expensive computations do not freeze the page. Communication between the main script and a worker happens through message passing with postMessage() and the onmessage event handler, rather than shared memory - workers cannot directly access the DOM.
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.Promises
A Promise represents a value that may not be available yet - the eventual result of an asynchronous operation. It exists in one of three states: pending, fulfilled, or rejected. then() handles a successful result, catch() handles an error, and finally() runs regardless of outcome. Promises are the foundation that async/await is built on top of.Fetch API
The Fetch API provides a modern, promise-based way to make HTTP requests, replacing the older XMLHttpRequest. fetch() returns a promise that resolves to a Response object once headers are received - note that a Response is only considered "ok" if the status is 2xx, so checking response.ok is essential, since fetch() does not reject on HTTP error statuses.Web Workers
Web Workers run JavaScript on a separate background thread, away from the main UI thread, so expensive computations do not freeze the page. Communication between the main script and a worker happens through message passing with postMessage() and the onmessage event handler, rather than shared memory - workers cannot directly access the DOM.