async iteration
Looping over values that arrive over time with for await (const x of source), waiting for each one before the next turn of the loop.
In more detail
Readable streams, fs.watch and many other APIs are async iterables. Because the loop only asks for the next item when the body finishes, a slow body naturally slows the source, and breaking out of the loop cleans the source up.
Related terms
- iterator — An object that produces the items of a sequence one at a time, on request, instead of all at once.
- Readable stream — A Node.js stream you read data from, such as a file being read, an incoming HTTP request or a socket.
- backpressure — A slow consumer signalling a fast producer to pause, so data does not pile up in memory faster than it can be handled.
Taught in
- Stream Mechanics: Readable Streams & Flowing Modes — Node.js Internals & Backend Services
- Directory Traversal: Recursive Readdir & fs.watch — Node.js Internals & Backend Services
- Native HTTP Server: Request & Response Lifecycles — Node.js Internals & Backend Services