Docs

README

18.5 Async Iterators & Streams

Overview

Async iterators and streams allow processing of data that arrives over time, perfect for handling large datasets, real-time data, or paginated APIs.

Async Iteration

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Sync vs Async Iteration                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                         β”‚
β”‚  SYNC ITERATION:                                                        β”‚
β”‚  Data is immediately available                                          β”‚
β”‚                                                                         β”‚
β”‚  for (const item of array) {                                            β”‚
β”‚      console.log(item);                                                 β”‚
β”‚  }                                                                      β”‚
β”‚                                                                         β”‚
β”‚  ASYNC ITERATION:                                                       β”‚
β”‚  Data arrives over time                                                 β”‚
β”‚                                                                         β”‚
β”‚  for await (const item of asyncSource) {                                β”‚
β”‚      console.log(item);                                                 β”‚
β”‚  }                                                                      β”‚
β”‚                                                                         β”‚
β”‚  Timeline:                                                              β”‚
β”‚  ───┬──────┬──────┬──────┬──────┬───▢ time                             β”‚
β”‚     β”‚      β”‚      β”‚      β”‚      β”‚                                       β”‚
β”‚  item 1  item 2  item 3  item 4  done                                   β”‚
β”‚                                                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Async Iterator Protocol

MethodReturnsDescription
[Symbol.asyncIterator]()Async IteratorReturns the async iterator
next()Promise<{value, done}>Returns next value
return() (optional)Promise<{done: true}>Cleanup on early exit
throw() (optional)PromiseHandle thrown errors

Creating Async Iterables

1. Async Generator Function

async function* fetchPages(url) {
  let page = 1;
  while (true) {
    const data = await fetch(`${url}?page=${page}`);
    const items = await data.json();

    if (items.length === 0) break;

    yield* items;
    page++;
  }
}

2. Custom Async Iterable

const asyncIterable = {
  async *[Symbol.asyncIterator]() {
    yield await fetchItem(1);
    yield await fetchItem(2);
    yield await fetchItem(3);
  },
};

Stream Concepts

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Data Stream Flow                                     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                         β”‚
β”‚  Source ──▢ Transform ──▢ Transform ──▢ Sink                           β”‚
β”‚    β”‚            β”‚            β”‚            β”‚                             β”‚
β”‚  produce      process      process     consume                          β”‚
β”‚                                                                         β”‚
β”‚  Examples:                                                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”                β”‚
β”‚  β”‚ API Data │──▢│  Parse  │──▢│ Filter  │──▢│ Displayβ”‚                β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β”‚
β”‚                                                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”                β”‚
β”‚  β”‚  File    │──▢│Decompress──▢│Transform│──▢│  Save  β”‚                β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β”‚
β”‚                                                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Web Streams API

Stream TypePurposeMethod
ReadableStreamSource of datagetReader()
WritableStreamDestinationgetWriter()
TransformStreamProcess datareadable, writable

Using for await...of

// With async generator
for await (const item of asyncGenerator()) {
  console.log(item);
}

// With readable stream
const reader = stream.getReader();
for await (const chunk of reader) {
  processChunk(chunk);
}

Backpressure

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Backpressure Control                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                         β”‚
β”‚  WITHOUT BACKPRESSURE:                                                  β”‚
β”‚  Producer: β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ά Fast                              β”‚
β”‚  Consumer: β–“β–“β–“β–“β–“β–“β–“β–“β–“β–Ά                 Slow                              β”‚
β”‚  Buffer:   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ ← OVERFLOW!                           β”‚
β”‚                                                                         β”‚
β”‚  WITH BACKPRESSURE:                                                     β”‚
β”‚  Producer: β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–Ά Slowed down                                   β”‚
β”‚  Consumer: β–“β–“β–“β–“β–“β–“β–“β–“β–“β–Ά   At its pace                                    β”‚
β”‚  Buffer:   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ      ← Controlled                                    β”‚
β”‚                                                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Summary

  • β€’Use async iterators for data over time
  • β€’for await...of simplifies consumption
  • β€’Web Streams for large data processing
  • β€’Handle backpressure to prevent memory issues
  • β€’Combine with generators for powerful patterns
README - JavaScript Tutorial | DeepML