Hiprup

What are Promise.all, allSettled, race, and any?

Four ways to coordinate multiple promises.

  • Promise.all — waits for all to fulfil; rejects as soon as one fails.

  • Promise.allSettled — waits for all and reports each result, success or failure.

  • Promise.race — settles as soon as the first promise settles (fulfils or rejects).

  • Promise.any — fulfils with the first success; rejects only if all fail.

Pick by need: all = "all must succeed", allSettled = "tell me every outcome", race = "first to finish", any = "first to succeed".

// Promise.all — all or nothing
const [user, posts] = await Promise.all([
  fetchUser(1),
  fetchPosts(1)
]); // Both must succeed

// Promise.allSettled — all results regardless
const results = await Promise.allSettled([
  fetchUser(1),
  fetchUser(999) // This might fail
]);
results.forEach(r => {
  if (r.status === 'fulfilled') console.log(r.value);
  else console.log('Failed:', r.reason);
});

// Promise.race — timeout pattern
const result = await Promise.race([
  fetch('/api/data'),
  new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000))
]);

// Promise.any — first success
const fastest = await Promise.any([
  fetch('https://server1.com/data'),
  fetch('https://server2.com/data'),
  fetch('https://server3.com/data')
]); // Uses whichever server responds first

all: parallel fetch, both must succeed. allSettled: check each result's status (fulfilled/rejected). race: the fetch or the timer — whichever settles first wins (timeout pattern). any: try three servers, use the first successful response (ignores failures).

Know all four and their use cases: all (parallel, fail-fast), allSettled (all results, no failure), race (first settled — timeout), any (first success). The timeout pattern with race is the most practical. allSettled is the one most candidates forget.

What are Promise.all, allSettled, race, and any? | Hiprup