Node.js Developer interview question · Most asked

When do you use `Promise.all`, `Promise.allSettled`, `Promise.race` and `Promise.any`?

Summary

all waits for every promise and rejects on the first failure; allSettled waits for all and reports each outcome; race settles with the first settled promise (fulfilled or rejected); any fulfills with the first success and rejects only if all fail (AggregateError).

Longer Answer

Use all when every result is required, allSettled for best-effort batches, race for timeouts (or better, AbortSignal.timeout()), and any for redundant sources (fastest mirror). For large batches, limit concurrency (e.g., p-limit) instead of firing thousands of requests at once.

Key Points

  • all: all-or-nothing
  • allSettled: every outcome
  • race: first settled
  • any: first success
  • Limit concurrency for large batches

More most-asked Node.js Developer questions

← All Node.js Developer questions