Intermediate JavaScript Interview QuestionsIntermediateComparison
JavaScript · Question 48
When should you use Promise.all(), Promise.allSettled(), Promise.any(), or Promise.race()?
Direct answer
Use Promise.all when all results are required, allSettled when every outcome matters, any when the first fulfillment is enough, and race when the first settlement—fulfillment or rejection—should decide the result.
Promise.all()fulfills with results in input order when all inputs fulfill; it rejects when an input rejects. That rejection does not automatically cancel the remaining operations.Promise.allSettled()waits for every input and fulfills with status records, making it useful for independent work where partial failure is expected.Promise.any()fulfills with the first successful value; it rejects with anAggregateErroronly when all inputs reject.Promise.race()settles as soon as the first input settles, whether that first result is fulfillment or rejection.
These methods accept iterables of values/Promises and return a Promise. They coordinate outcomes; they do not themselves make CPU-bound JavaScript execute on multiple CPU cores.
A dashboard loading independent widgets might prefer allSettled. A request requiring user, permissions, and configuration together might prefer all. Multiple equivalent mirrors can be a fit for any.