A developer builds a Node.js microservice that calls three other services to compose a response. All three calls are independent. The code currently uses sequential await calls. A performance review flags this.
What is the correct pattern to call three independent async APIs in parallel in Node.js, and what is the performance gain?
- A.Use Promise.all([serviceA(), serviceB(), serviceC()]) to initiate all three requests concurrently; total wait time equals the longest single request rather than the sum of all three, reducing latency proportionally to the number of sequential calls
- B.Use Promise.allSettled() to retry failed promises until all three succeed
- C.Create three separate Express.js instances and load-balance between them
- D.Use setTimeout with a 0ms delay to convert sequential calls to parallel execution
Why A is correct
Sequential awaits (await a(); await b(); await c()) wait for each to complete before starting the next - total time = sum of all durations. Promise.all([a(), b(), c()]) fires all three concurrently; total time = max(a, b, c). For three 200ms services: sequential = 600ms, parallel = ~200ms. Promise.allSettled() is similar but continues even if one rejects, returning fulfilled/rejected status per promise. Security note: parallel fan-out can inadvertently amplify downstream load - if each of 1000 concurrent users triggers 3 parallel calls, the downstream services see 3000 concurrent requests.
Know someone studying for Web App Fundamentals? Send them this one.