Skip to content

Latest commit

 

History

History
40 lines (26 loc) · 1.16 KB

File metadata and controls

40 lines (26 loc) · 1.16 KB

71. Readable streams and backpressure

Learn

  • ReadableStream
  • reader
  • chunks
  • backpressure
  • cancel

How it works

A readable stream exposes chunks incrementally. A reader can process each chunk and stop or cancel. Backpressure coordinates producers and consumers so a fast source does not overwhelm slower processing. Use streaming for large or incremental data; a small JSON response is usually simpler to parse all at once.

Common mistake

Reading every chunk into one large array defeats the memory benefit of streaming.

Guided challenge

Create a ReadableStream that emits three text chunks and read them one at a time, then cancel a second stream early.

Further reading

Run the example

Open example.html, inspect example.js, then edit and refresh. For APIs that need an origin, run a local server from the JavaScripts folder.

Completion check

  • Explain the behavior and its limits in your own words.
  • Test a normal case and a failure or cleanup case.
  • Record when this feature is useful and when a simpler approach is better.