JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Promises & Async/Await

javascript
mid
async

What are Promise.all, Promise.race, Promise.allSettled, and Promise.any, and when do you use each?

promises
async
promise-all
promise-race
concurrency
Quick Answer

Promise.all resolves when all promises succeed (fails fast on first rejection). Promise.race resolves/rejects with the first settled promise. Promise.allSettled waits for all to settle regardless of outcome. Promise.any resolves with the first fulfilled promise (ignores rejections unless all fail).

Detailed Explanation

JavaScript provides four static methods for composing multiple Promises:

Promise.all(promises)

  • Resolves when all promises fulfill
  • Rejects immediately if any promise rejects (fail-fast)
  • Returns array of results in order
  • Use for: parallel independent operations that all must succeed

Promise.race(promises)

  • Settles with the first promise to settle (fulfill or reject)
  • Use for: timeouts, fastest response wins

Promise.allSettled(promises) (ES2020)

  • Waits for all promises to settle
  • Never rejects — returns array of {status, value/reason} objects
  • Use for: batch operations where you need all results regardless of failures

Promise.any(promises) (ES2021)

  • Resolves with the first fulfilled promise
  • Rejects only if all promises reject (AggregateError)
  • Use for: redundant sources, fastest success wins

Code Examples

Promise.all and Promise.allSettledJavaScript
const fetchUser = fetch('/api/user').then(r => r.json());
const fetchPosts = fetch('/api/posts').then(r => r.json());
const fetchComments = fetch('/api/comments').then(r => r.json());

// Promise.all — all must succeed
try {
  const [user, posts, comments] = await Promise.all([
    fetchUser, fetchPosts, fetchComments
  ]);
  // All three succeeded
} catch (error) {
  // Any one failed — don't know which succeeded
}

// Promise.allSettled — get all results
const results = await Promise.allSettled([
  fetchUser, fetchPosts, fetchComments
]);
results.forEach(result => {
  if (result.status === 'fulfilled') {
    console.log('Success:', result.value);
  } else {
    console.log('Failed:', result.reason);
  }
});

Real-World Applications

Use Cases

Dashboard Loading

Using Promise.all to fetch multiple widget datasets in parallel for a dashboard that displays several data sources

Health Checks

Using Promise.allSettled to check status of multiple microservices without failing fast on a single outage

CDN Failover

Using Promise.any to get data from the fastest responding CDN mirror, ignoring slower or failing ones

Mini Projects

Service Health Dashboard

beginner

Build a dashboard that checks multiple API endpoints using Promise.allSettled and displays status for each

Request Timeout Wrapper

intermediate

Build a fetch wrapper using Promise.race that rejects with a timeout error after a configurable duration

Industry Examples

Next.js

Parallel data fetching in server components using Promise.all for multiple concurrent fetch calls

Cloudflare Workers

Developers use Promise.any to implement failover patterns across multiple origin servers in edge functions

Resources

MDN - Promise.all()

docs

MDN - Promise.allSettled()

docs

JavaScript.info - Promise API

article

Related Questions

What are Promises in JavaScript and how do they work?

mid
async
Previous
What is the difference between microtasks and macrotasks in JavaScript?
Next
How do debounce and throttle work, and when would you use each?
PrevNext