QA
Intermediate LevelJavaScript & TypeScript

How do Promise.all(), Promise.race(), Promise.allSettled(), and Promise.any() work?

❓ Interview Prompt: Compare Promise combinators for parallel, first-completed, and fault-tolerant async patterns.

Promise Combinators:

1
`Promise.all(promises)`: Waits for ALL promises to fulfill. Rejects immediately if ANY one rejects.
2
`Promise.allSettled(promises)`: Waits for ALL promises to settle (fulfill or reject). Never short-circuits. Returns status and value/reason for each.
3
`Promise.race(promises)`: Returns the result of the FIRST promise to settle (fulfill or reject).
4
`Promise.any(promises)`: Returns the FIRST fulfilled promise. Ignores rejections unless ALL reject (throws AggregateError).

Code Implementation Example

javascript
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());

// All must succeed
const [user, posts, comments] = await Promise.all([
  fetchUser, fetchPosts, fetchComments
]);

// Fault-tolerant: get status of each
const results = await Promise.allSettled([
  fetchUser, fetchPosts, fetchComments
]);
results.forEach(r => {
  if (r.status === 'fulfilled') console.log(r.value);
  else console.error(r.reason);
});
💡Promise.all for parallel requests and allSettled for fault tolerance.
🎯 Interview Key Takeaway

Use Promise.all for parallel dependent requests. Use Promise.allSettled when partial failure is acceptable.

#Promises#Parallel#Combinators#Intermediate
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?