KatInterview

Promise Concurrency Combinators: all, any, allSettled

System DesignModel answer
MediumModel answerSystem Designjavascriptpromisesconcurrency

Last updated

Compare how Promise.all, Promise.any, and Promise.allSettled settle, handle failures, and order results, then pick the right one for three real service-orchestration scenarios.

Background

You're building a backend-for-frontend (BFF) layer that fires off concurrent requests to several downstream services. JavaScript gives you three built-in combinators for coordinating a group of promises: Promise.all, Promise.any, and Promise.allSettled. Choosing the wrong one leads to silent failures, unnecessarily aborted requests, or stalled responses.

What you need to cover

Part 1 — Behavioral comparison

Walk through how the three combinators differ across these five dimensions:

  • Resolution trigger: under what condition does the returned promise fulfill?
  • Rejection trigger: under what condition does it reject, and what does the rejection value look like?
  • Resolved value shape: what does a successful result actually contain?
  • Ordering: do results reflect the input order or the order in which promises settled?
  • Empty input: what happens when you pass [] to each one?

Part 2 — Lifecycle and failure semantics

Address both of the following:

  • When Promise.all short-circuits on the first rejection, or Promise.any short-circuits on the first fulfillment, what happens to the other in-flight promises? Does the runtime cancel the underlying network I/O?
  • Where do unhandled promise rejection warnings actually come from when working with these combinators, and how do you prevent them?

Part 3 — Scenario matching

For each use case below, name the best combinator and explain why the others would be wrong:

  1. Widget dashboard: a page loads data for five independent widgets. A single failing widget should show an inline error; the other four must still display their data.
  2. Payment validation: a purchase requires sign-off from a fraud service, a stock service, and a funds service. If any one of them rejects or returns a denial, the whole transaction must abort as fast as possible.
  3. Geo-redundant asset fetch: the same config file is served from three CDN regions. You want whichever region responds successfully first; a region that returns an HTTP error should not "win."

Hints

Hint 1

Think about what 'all' and 'any' mean in set logic when the input set is empty — that should tell you exactly what Promise.all([]) and Promise.any([]) return without memorizing the spec.

Hint 2

Short-circuiting means the combinator stops caring about the remaining promises, not that those promises stop running. What would you need to add to actually cancel in-flight fetch requests?

Hint 3

Promise.race and Promise.any look similar for the CDN scenario — think carefully about what each one does when the fastest promise rejects rather than fulfills.