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-nothingallSettled: every outcomerace: first settledany: first success- Limit concurrency for large batches
More most-asked Node.js Developer questions
- What is the difference between `process.nextTick`, Promise microtasks, `setImmediate` and `setTimeout(fn, 0)`?
- What are streams in Node.js and what is backpressure?
- Walk through how an Express app handles a request, including errors from async handlers.
- Explain the phases of the Node.js event loop.
- What is Node.js and why is it well suited to I/O-heavy servers?
- How has asynchronous code evolved in Node.js: callbacks, Promises and async/await?
- Why is CPU-bound work a problem in Node.js and how do you handle it?
- What are the differences between CommonJS and ES modules in Node.js?