5. You want to fetch data from two APIs in Node.js and combine results. Which async pattern best ensures both calls run at the same time and you wait for both results before continuing?
hard
A. Use Promise.all with both API calls and await the combined promise.
B. Call both APIs without await and process results immediately.
C. Call one API, await it, then call the second API and await it.
D. Use setTimeout to delay the second API call after the first.
Solution
Step 1: Understand sequential vs parallel calls
Awaiting one API before calling the second runs them sequentially, slowing total time.
Step 2: Use Promise.all for parallel execution
Promise.all runs both calls simultaneously and waits for both to finish before continuing.
Final Answer:
Use Promise.all with both API calls and await the combined promise. -> Option A
Quick Check:
Promise.all runs async calls in parallel [OK]
Hint: Use Promise.all to await multiple async calls together [OK]