Async JavaScript

Async Iteration & Streams

Async iterables represent values that arrive over time, and streams represent chunked data flow with backpressure. They are useful when data is large, incremental, or unbounded.
  • Async iterables are consumed with for await...of
  • Streams process chunks instead of whole payloads
  • Backpressure prevents memory blowups
  • Chunk boundaries are not semantic boundaries
  • Collecting everything defeats streaming
Consume async chunks
for await (const chunk of stream) {
  await processChunk(chunk)
}
The loop waits for each chunk and can apply backpressure through awaited processing.
Streaming concepts
ConceptMeaningExample
Async iterableValues arrive over timefor await...of
Readable streamSource of chunksHTTP response body
Writable streamSink for chunksFile upload target
Transform streamChunk-to-chunk processingCompression / parsing
BackpressureConsumer slows producerAvoid memory growth
Sources
  • MDN JavaScript Guide and ReferenceAsync iterators
  • Node.js Documentation and Learn Node.jsStreams and backpressure
  • The Modern JavaScript TutorialAsync iterators and generators