Course contentsShow
← JavaScript
Lesson 1316 of 2,72329. PromisesFree lesson

Comparing any, race, all, and allSettled

Understand when to use each combinator based on your success and failure needs.

Comparing any, race, all, and allSettled

You'll learn when to choose the right Promise combinator based on what success and failure mean for your use case.

Now that you've seen all four Promise combinators, it's time to understand when to use each one. The key is asking yourself two questions: Do I need all promises to succeed? and Do I care about failures immediately or only at the end?

Decision Framework

Promise.all — Use when you need every promise to succeed. If any fails, you want to bail out immediately. Perfect for: fetching required data from multiple sources, running parallel validations where all must pass, or executing independent tasks that are all mandatory.

Promise.race — Use when you only care about the first result, success or failure. The fastest promise wins, and you ignore the rest. Perfect for: implementing timeouts, racing multiple data sources (CDNs), or testing response times.

Promise.allSettled — Use when you need to try everything and see all outcomes, regardless of success or failure. You want a complete report card. Perfect for: batch operations where partial success is acceptable, running health checks on multiple services, or sending notifications where some might fail.

Promise.any — Use when you need at least one success, and you don't care which one. The first fulfillment wins; failures are collected only if everything fails. Perfect for: trying multiple mirrors of the same resource, implementing fallback strategies, or finding the first available service.

Mental Model

Think of it like ordering pizza from multiple places:

  • all: Every restaurant must deliver or you cancel the party
  • race: You eat whatever arrives first (even if it's bad)
  • allSettled: You wait for all deliveries to see what you got
  • any: You eat the first edible pizza; only panic if all are inedible

Key Takeaway: Choose all for required parallel work, race for speed competitions, allSettled for complete reports, and any for "first success" scenarios.