# Promise Combinators

> What are the four Promise combinator methods? When would you use each?

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-promise-combinators
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

What are the four Promise combinator methods? When would you use each?

## Answer

Promise.all(promises): resolves when ALL resolve, rejects immediately on any rejection. Use when you need all results and one failure should cancel all.

Promise.allSettled(promises): waits for ALL to settle. Returns array of { status, value/reason }. Use when you want all results regardless of failures.

Promise.race(promises): resolves/rejects with the first to settle. Use for timeouts or first-to-respond patterns.

Promise.any(promises): resolves with the first successful promise. Rejects only if ALL reject (AggregateError). Use when any one success is enough.

**all, allSettled, race and any on the same promises. Change each promise's timing and outcome, then switch combinators under Tweak.**

1. **Promise.all**: Fulfils with every value in order, or rejects at the first rejection.
2. **Promise.allSettled**: Waits for everything and reports each status. Never rejects.
3. **Promise.race**: Settles with whichever promise settles first, success or failure.
4. **Promise.any**: Fulfils with the first success; rejects with AggregateError only if all fail.
5. **Nothing is cancelled**: The losing promises keep running after the combinator settles.

## Code examples

### Promise.all vs Promise.allSettled

```
const fetchUser = () => Promise.resolve({ id: 1, name: 'Alice' });
const fetchPosts = () => Promise.resolve([{ id: 1 }]);
const fetchBroken = () => Promise.reject(new Error('Network error'));

try {
  const [user, posts] = await Promise.all([
    fetchUser(),
    fetchPosts(),
    fetchBroken()
  ]);
} catch (e) {
  console.log(e.message);
}

const results = await Promise.allSettled([
  fetchUser(),
  fetchPosts(),
  fetchBroken()
]);

results.forEach(result => {
  if (result.status === 'fulfilled') {
    console.log('Success:', result.value);
  } else {
    console.log('Failed:', result.reason.message);
  }
});
```

Promise.all short-circuits on first failure. Promise.allSettled always waits for all and provides status for each.

### Promise.race and Promise.any

```
function timeout(ms) {
  return new Promise((_, reject) =>
    setTimeout(() => reject(new Error(`Timeout after ${ms}ms`)), ms)
  );
}

try {
  const result = await Promise.race([
    fetch('/api/data').then(r => r.json()),
    timeout(3000)
  ]);
  console.log('Result:', result);
} catch (e) {
  console.log(e.message);
}

const servers = ['https://s1.api.com', 'https://s2.api.com', 'https://s3.api.com'];

try {
  const fastestResponse = await Promise.any(
    servers.map(url => fetch(url).then(r => r.json()))
  );
  console.log('Fastest:', fastestResponse);
} catch (e) {
  console.log('All failed:', e.errors);
}
```

Promise.race is ideal for timeouts. Promise.any is ideal for redundant requests - use whichever server responds first successfully.

## Key points

- Promise.allSettled added in ES2020; Promise.any in ES2021
- Promise.any rejects with AggregateError (contains an errors array) when all reject
- Promise.race can be used for any 'first to respond' scenario
- Empty array: Promise.all(\[\]) resolves to \[\]; Promise.any(\[\]) rejects immediately

## Common follow-up questions

- How do you implement a timeout for a fetch request?
- What is an AggregateError and when does it occur?
- What does Promise.all(\[\]) return?

## Interview tips

- The all vs allSettled distinction is the most common question - nail the difference
- Show the timeout pattern with Promise.race - practical and elegant
- Know that Promise.any rejects with AggregateError, not a regular Error

## Concepts covered

Promise.all, Promise.allSettled, Promise.race, Promise.any, Concurrency

Previous: [ES Modules vs CommonJS](https://www.internhack.in/learn/interview/javascript-interview/js-modules-advanced) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Data Types & Type Checking](https://www.internhack.in/learn/interview/javascript-interview/js-data-types)
- [Operators & Short-Circuit Evaluation](https://www.internhack.in/learn/interview/javascript-interview/js-operators-short-circuit)
- [String Methods & Template Literals](https://www.internhack.in/learn/interview/javascript-interview/js-string-methods)
- [Array Methods (map, filter, reduce)](https://www.internhack.in/learn/interview/javascript-interview/js-array-methods)
- [Destructuring & Spread/Rest](https://www.internhack.in/learn/interview/javascript-interview/js-destructuring-spread)
- [Error Handling (try/catch/finally)](https://www.internhack.in/learn/interview/javascript-interview/js-error-handling)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
