QuestionsJavaScript

Promise.all vs allSettled vs race vs any

Promise CombinatorsMediumJavaScript

Compare the four Promise combinators. When do you reach for each, and how does failure behave?

What it tests

Choosing the right aggregation semantics — especially all-or-nothing vs collect-all-outcomes.

Approach & answer

All four take an iterable of promises and return one promise, but they settle differently. Promise.all resolves to an array of all values IF every input resolves; it rejects IMMEDIATELY on the first rejection (fail-fast) — use it when you need all results and any failure invalidates the whole batch. Note the others keep running even after all() rejects (there's no cancellation in JS promises). Promise.allSettled NEVER rejects: it waits for every input and resolves to an array of `{status:'fulfilled', value}` / `{status:'rejected', reason}` objects — use it when you want every outcome regardless of individual failures (e.g. fire N independent requests and render partial results). Promise.race settles as soon as the FIRST input settles, adopting its value OR rejection — use for timeouts (race real work against a reject-after-Xms promise) or first-response-wins. Promise.any resolves with the first FULFILLED value, ignoring rejections; it rejects only if ALL inputs reject, with an AggregateError whose `.errors` holds every reason — use for redundancy (try several mirrors, take whichever succeeds first). Mnemonic: all = every value or first error; allSettled = every outcome; race = first to settle either way; any = first success or all-failed AggregateError.

Use this technique when

Fan-out requests (all/allSettled), timeouts and first-wins (race), and redundant/fallback sources (any).

References

js