liveBook · Manning

ReadableStream

As explained in JavaScript in Depth

A Web Streams API object representing a source from which data can be read incrementally.

A ReadableStream wraps a data source and delivers chunks through a consumer-driven pull cycle. Unlike Node.js Readable streams, which mostly push through events, it moves nothing until a consumer requests data. section 10.4 section 10.4.1

The underlying source supplies pull(controller). The stream invokes pull() when its internal queue has room; the source calls controller.enqueue(), and a pending reader.read() promise resolves. This structural backpressure keeps the producer from outrunning the consumer, provided code does not bypass the normal path. section 10.4.1

The pull cycle

const readable = new ReadableStream({ pull(controller) { controller.enqueue('hello'); } }); const reader = readable.getReader(); const { value, done } = await reader.read(); The read request checks the queue. If it is empty or below its highWaterMark, pull() runs; enqueue() supplies the chunk and resolves the read. section 10.4.1 section 10.4.1

Use the model with these constraints:

  • Treat each chunk as passing through at least one promise. This is usually negligible for network I/O but can add allocation and microtask overhead for synchronous, high-throughput sources. section 10.4.1
  • Distinguish Web Streams from Node.js streams: ReadableStream uses pull-based scheduling and promise rejection, while Node.js Readable commonly uses push-based flow, buffers, and error events. section 10.4 section 10.6.1
  • Remember that Web Streams are portable across browsers, Node.js, Deno, and Cloudflare Workers, although runtimes may optimize the standard's procedural operations differently. section 10.4 section 10.2

Errors in the underlying source move the stream into an errored state, causing subsequent reads to reject. Handle the reader promise or await the promise returned by pipeTo(); otherwise failure may become an unhandled rejection. section 10.6.1

How JavaScript in Depth covers it

How deep it goes
Traces source pulling, queueing, promise costs, backpressure, errors, and runtime optimization, but not full standard algorithms.
Examples
Uses an underlying source with pull(), controller.enqueue(), getReader(), read(), and a documented pull-cycle diagram.
What it assumes
Assumes promises and asynchronous reading, while teaching the pull model and its contrast with Node.js push streams.
What you can do after
Apply it via the Web Streams API, read chunks incrementally, respect pull-based backpressure, and catch rejected reads.
How it connects
Links ReadableStream to WritableStream, TransformStream, backpressure, promises, async iterables, and Node.js streams.

Also explained in

Compare how 2 books explain ReadableStream →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.