Promise.all: Examples and Use Cases in JavaScript
Promise.all runs promises in parallel and waits for all. Here are practical examples.
Promise.all: Examples and Use Cases in JavaScript
Promise.all runs promises in parallel and waits for all to fulfill. Here are practical examples.
Fetch Multiple Resources
const [user, posts, comments] = await Promise.all([ fetchUser(id), fetchPosts(), fetchComments(), ]);
Fetch Multiple Pages
const pages = await Promise.all( [1, 2, 3, 4, 5].map((page) => fetch(`/api?page=${page}`).then((r) => r.json())) ); `` ### Parallel Data Transformation ```js const [processedA, processedB] = await Promise.all([ processA(data), processB(data), ]); `` ### Timeout with Promise.race ```js function withTimeout(promise, ms) { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), ms) ); return Promise.race([promise, timeout]); } const result = await withTimeout(fetch("/api"), 5000); `` ### Fast Fail ```js // if any fetch fails, the whole thing fails immediately try { const [a, b, c] = await Promise.all([fetchA(), fetchB(), fetchC()]); } catch (err) { console.error("One of the fetches failed:", err); } `` ### Empty Array ```js const results = await Promise.all([]); // [] (resolves immediately) `` ### Non-Promise Values ```js const [a, b, c] = await Promise.all([1, Promise.resolve(2), 3]); // [1, 2, 3] (non-promises are passed through) `` ### The Takeaway `Promise.all` is for parallel independent operations. Use cases: fetching multiple resources, fetching multiple pages, parallel data transformation, and fast-fail scenarios. Empty arrays resolve immediately. Non-promise values are passed through. Results are in order.
It runs all promises in parallel and waits for all to fulfill. If any rejects, the whole thing rejects (fast fail). Results are in the same order as the input promises. Non-promise values are passed through.
Yes. Promise.all rejects as soon as any promise rejects (fast fail). The other promises continue running (they are not cancelled), but their results are ignored. Use Promise.allSettled if you want all results regardless of failures.
An empty array []. Promise.all with an empty array resolves immediately (there is nothing to wait for). This is useful when you dynamically build an array and it might be empty.
Yes. Non-promise values are passed through as-is. Promise.all([1, Promise.resolve(2), 3]) resolves to [1, 2, 3]. This is useful for mixing sync and async values.
Race the operation against a timeout promise: Promise.race([fetch('/api'), new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 5000))]). If the timeout fires first, the race rejects.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

