Node.jsQuestion 11 of 26
Dependent API calls
Answer out loud or in your head first, then compare.
Answer
If a call needs the result of the previous one, I run them in sequence. I only run independent work in parallel.
JavaScript
const user = await getUser(email);
const orders = await getOrders(user.id);
const details = await Promise.all(
orders.map(order => getDetails(order.id))
);To be precise: for thousands of items I limit concurrency so I don't overload the API or the database. I also define what happens on a partial failure.
Pitfall: array.forEach(async () => ...) doesn't wait for the callbacks to finish. I use for...of for a sequence, or map with the right Promise combinator.
How did it go? Be honest — “Not yet” puts the question on your review list.