Why might Promise.all() be the wrong tool when consuming many async iterators?
Short answer
Promise.all() waits for ALL promises to settle before giving you anything, so if you're driving async iterators through it you lose the ability to process results as they arrive — defeating the point of streaming. Use for-await-of or manual racing for incremental results.
Advertisement
AModel answer
Promise.all() waits for ALL promises to settle before giving you anything, so if you're driving async iterators through it you lose the ability to process results as they arrive — defeating the point of streaming. Use for-await-of or manual racing for incremental results.
// Wrong: loses streaming benefit
const all = await Promise.all(iterators.map(it => it.next()));
// Better: process each as it resolves
for await (const v of mergeAsyncIterables(...iterators)) { /* ... */ }Real-world example
Showing search results from multiple providers as each one responds, instead of waiting for the slowest.
Likely follow-ups
- When IS Promise.all() the right choice for iterator-like data?
Related topics