# Promise Combinators

> Learn how to orchestrate multiple background tasks at the same time. Master Promise.all, Promise.race, and their modern counterparts.

Subject: JavaScript | Lesson 34 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/promise-combinators
Previous: [async / await](https://codewithanimation.in/javascript/async-await.md) | Next: [Fetch, AJAX & HTTP](https://codewithanimation.in/javascript/fetch-and-ajax.md)

## The Big Four

When you have multiple independent asynchronous tasks, you shouldn't wait for them one by one. You should fire them all off at the same time.

JavaScript provides four static methods on the `Promise` object - known as combinators - to help you coordinate arrays of Promises.

## The 'All or Nothing' Approach

`Promise.all([p1, p2])` is the most common combinator. It waits for *every single promise* in the array to fulfill.

If they all succeed, it returns an array of all the results. However, it is an 'all or nothing' deal: if even ONE promise rejects, the entire `Promise.all` immediately rejects, ignoring the rest.

## The Need for Speed

`Promise.race([p1, p2])` acts like a literal foot race. It resolves or rejects the moment the *very first* promise settles.

It is commonly used to implement network timeouts (racing a `fetch` request against a `setTimeout` that rejects after 5 seconds).

## Common mistake: The Strictness of all()

Because `Promise.all` fails completely if a single item fails, it is a bad choice for fetching non-critical data. If you are loading a user's dashboard and their 'friend suggestions' request fails, you don't want the entire dashboard to crash!

## Tip: The Modern Additions

To fix the strictness of `all`, ES2020 introduced `Promise.allSettled()`. It waits for all promises to finish, regardless of success or failure, returning an array detailing the outcome of each.

ES2021 introduced `Promise.any()`, which is like `race`, but it ignores rejections and returns the first *successful* promise.

## Try it: All vs Race

Examine this visual breakdown of how the two most common combinators coordinate multiple background tasks.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/promise-combinators

## Check yourself

### Question 1

If you pass an array of 5 promises into `Promise.all()`, and the 3rd one rejects after 1 second, what happens?

- A. The entire Promise.all rejects immediately at the 1-second mark.
- B. It waits for the other 4 to finish, then rejects.
- C. It returns an array with 4 successes and 1 error.

Correct answer: A

`Promise.all` is strictly 'all or nothing'. A single rejection causes an immediate failure of the entire group.

- Why not B: Incorrect. `Promise.all` short-circuits and fails fast.
- Why not C: Incorrect. That is how `Promise.allSettled()` behaves, not `all()`.

### Question 2

Which combinator is best for implementing a timeout (e.g., aborting a request if it takes longer than 5 seconds)?

- A. Promise.race()
- B. Promise.any()
- C. Promise.all()

Correct answer: A

By 'racing' your fetch request against a 5-second timer that rejects, whichever finishes first determines the final outcome.

- Why not B: Incorrect. `any` ignores rejections, so your 5-second timeout rejection would be ignored!
- Why not C: Incorrect. `all` waits for the fetch to finish, defeating the purpose of a timeout.

### Question 3

If you need to fetch data from 3 different APIs, and you want to show whatever data succeeds even if 1 API goes down, which combinator should you use?

- A. Promise.allSettled()
- B. Promise.all()
- C. Promise.any()

Correct answer: A

`allSettled` waits for everything to finish and gives you a report on each, allowing you to gracefully handle the failures while still displaying the successes.

- Why not B: Incorrect. `all` would crash the whole page if 1 API went down.
- Why not C: Incorrect. `any` only returns the FIRST success, ignoring the other 2 successful APIs.

## Remember this

- Promise combinators allow you to coordinate arrays of asynchronous tasks.
- `Promise.all` waits for ALL to succeed, but fails immediately if ONE rejects.
- `Promise.race` takes the VERY FIRST promise to settle, whether it succeeded or failed.
- `Promise.allSettled` waits for ALL to finish, never rejecting.
- `Promise.any` takes the FIRST success, only rejecting if they ALL fail.
