Skip to content

Async stream

Category: Async · Status: stub · Lessons: chapter 06, Async (planned)

One line: An asynchronous iterator: a sequence whose items arrive over time, each one awaited in turn.

Also called: Stream, async generator, AsyncIterator, Sink.

How it connects

flowchart LR
  n_async_stream["Async stream"]
  n_future_and_promise["Future and promise"]
  n_reactive_programming["Reactive programming"]
  n_async_stream -->|uses| n_future_and_promise
  n_reactive_programming -->|uses| n_async_stream
  classDef center stroke-width:3px
  class n_async_stream center
  classDef outside stroke-dasharray: 4 3
  class n_future_and_promise,n_reactive_programming outside

In each language

Rust no stable trait: std's AsyncIterator is a nightly-only experimental API
Java Flow.Publisher, with the subscriber requesting items
Python asynchronous generators ↗, consumed with async for
C# IAsyncEnumerable<T>, consumed with await foreach
JavaScript async function* generators, consumed with for await...of
Kotlin Flow: a cold flow is lazy and runs anew for each collector, a hot flow shares its values with all collectors
Swift AsyncSequence, iterated with for await; an AsyncStream builds one from a closure

Where to read more