JavaScript · Question 90
When should you use for await...of instead of Promise.all()?
Direct answer
Use Promise.all() when you already have a finite set of independent operations whose results can be awaited together; use for await...of when values arrive over time through an async iterable, when you need incremental processing, or when backpressure-like sequential consumption matters.
If you have ten independent fetches that should start together and you need all results, Promise.all() expresses that intent well. By contrast, an async iterable may produce values only as data becomes available, perhaps from pages, streams, queues, or an async generator. for await...of consumes those values incrementally.
Using for await...of over a source does not automatically mean the underlying work is sequential; that depends on how the async iterator produces values. But the loop itself awaits each iteration result before advancing, giving the producer and consumer a natural coordination point.
- Finite independent batch → often
Promise.all(). - Potentially long-lived or incremental async sequence → often
for await...of. - If you need bounded parallelism over a stream, combine async iteration with an explicit concurrency-control strategy rather than choosing between only “one at a time” and “everything at once.”