JavaScript Course
JavaScript
/
Advanced

Promise.all / race / any / allSettled

Definition

Static methods on the Promise object that allow you to handle multiple asynchronous operations simultaneously, rather than awaiting them one by one.

Explain Like I'm New

Imagine sending 3 friends out to grab food. `Promise.all`: You wait until ALL friends return before eating. If one gets a flat tire (rejects), dinner is ruined. `Promise.race`: You eat the food of whoever gets back FIRST. `Promise.any`: You eat the first food that arrives safely, ignoring flat tires. `Promise.allSettled`: You wait for everyone to return, whether they brought food or excuses.

Real World Example

Using `Promise.all` to fetch data from three completely independent APIs at the exact same time, cutting a 6-second total loading time down to 2 seconds.

Common Use Cases

  • •Parallel execution
  • •Performance optimization
  • •Graceful failure handling

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Why is `Promise.all` faster than sequential `await`?

intermediate

  • What happens if one Promise in `Promise.all` rejects?

advanced

  • When would you choose `Promise.allSettled` over `Promise.all`?

Flash Cards

Question

What happens if one Promise in Promise.all rejects?

Click to reveal answer
Answer

The ENTIRE `Promise.all` immediately rejects with that single error. The other promises continue running in the background, but their results are completely ignored.

Question

When to choose allSettled?

Click to reveal answer
Answer

If the independent API calls don't strictly rely on each other. If you are fetching a user's 'Recent Posts' and 'Recommended Friends', and the Friends API fails, you still want to show the Posts. `allSettled` guarantees you get an array back showing the `{ status: 'fulfilled' / 'rejected' }` of every request.