Async iterators and for await...of for streaming
streaming vs buffering everything.
async iterators yield values lazily over time; for await...of consumes them sequentially with backpressure, keeping memory bounded.
WHAT THIS TESTS This evaluates whether you understand lazy, sequential async consumption and when streaming beats buffering everything into memory.
A GOOD ANSWER COVERS An async iterable exposes a method returning an async iterator whose next call resolves to a Promise of the next value and a done flag. The for await...of loop drives that iterator: it awaits each yielded value, runs the loop body, and only then asks for the next value. This gives sequential processing with natural backpressure, because the producer is not forced to run ahead of the consumer, and memory stays bounded since you hold one item at a time rather than the whole dataset. Node streams are async iterable, so you can iterate a readable stream directly. Contrast this with Promise.all, which requires you to create every Promise up front, so all results coexist in memory simultaneously, which is fine for small bounded sets but catastrophic for very large or unbounded sources.
COMMON WRONG ANSWERS Claiming for await...of runs items in parallel like Promise.all, ignoring the memory blowup of buffering huge datasets, or thinking async iterators are merely syntactic sugar with no streaming benefit. Forgetting that the loop is inherently sequential, which is a tradeoff against throughput.
LIKELY FOLLOW-UPS How to add bounded parallelism on top of an async iterator, how async generators produce these iterators, how Node readable streams implement the protocol, and how to handle errors and cleanup mid-iteration.
ONE CONCRETE EXAMPLE Processing a multi-gigabyte log file: you create a readline interface over a read stream and iterate it with for await...of, handling each line and discarding it before the next arrives, so memory usage stays flat regardless of file size. Doing this with Promise.all would require reading every line into an array first, which could exhaust memory. Similarly, an async generator that yields one page of a paginated API per iteration lets you process results page by page without ever holding the entire result set at once.
Read the original → developer.mozilla.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.