Streams support a cool thing called backpressure. This amazing concept basically enables two-way communication within a stream, where the consumer of the stream can speak to its source and be like HEY MAN SLOW DOWN I CAN'T PROCESS THINGS THAT FAST.
Basically, you give a stream a maxiumum number of things it can hold in its underlying sink, any more than that, and it says dude, wait.
Here's an example.
Basically, we try and write to our WritableStream 5000 times in quick succession. We've configured the WritableStream to only be able to handle 1000 things in its queue, using what is called a CountQueuingStrategy's highWaterMark, which is the point at which the WritableStream starts applying backpressure, saying back off, bro.
When you click Start, you should be able to see it all in action.
Each time the progress bar stops, the WritableStream is applying backpressure.
More Examples 🎉
- Streaming Progressive Web Applications
- Basically, Backpressure
- Basically, Fetch (w/ progress bars!)
- Basically, Forking Streams
Further Reading: WhatWG Spec