Chain nested fetches per item
Chain each item's dependent fetch so a slow item only delays itself.
From vercel-react-best-practices, rule server-parallel-nested-fetching.
One slow chat holds the rest
Press Load chats, then drag the slow chat's latency.
Two Promise.all passesBad
0 ms0 / 6 authors
Chained per itemGood
0 ms0 / 6 authors
const chatAuthors = await Promise.all(
chatIds.map((id) => getChat(id).then((chat) => getUser(chat.author)))
)
Why it matters, pros and cons
Why it matters
Two passes put a barrier between steps, and the slowest item sets it for all.
Pros
- Each item is done when its own data is.
- Same number of requests, one-line change.
- Pairs well with streaming each item.
Cons
- Total time is still the slowest chain.
- No gain when every item takes about as long.
- A batch call like
getUsers(ids)may beat N calls.