Backpressure and Piping
Manage flow control with pipeTo and pipeThrough.
Backpressure and Piping is a free JavaScript Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.
The Backpressure Problem
If a producer generates data faster than a consumer can handle, memory fills up. Backpressure is the mechanism by which a slow consumer tells a fast producer to slow down.
WritableStream
A WritableStream is a destination you write chunks into. Its write(chunk) sink method may be async, signaling readiness for the next chunk.
const sink = new WritableStream({
write(chunk) {
console.log("writing", chunk);
}
});Getting a Writer
getWriter() locks the writable and returns a writer with write(), close(), and a ready promise.
const writer = sink.getWriter();
await writer.write("hello");
await writer.close();The ready Signal
Await writer.ready before writing. It resolves when the stream can accept more data — this is backpressure in action.
await writer.ready;
await writer.write(chunk);pipeTo
readable.pipeTo(writable) connects a source to a destination and automatically manages backpressure for you. It returns a promise that resolves when piping completes.
await readableStream.pipeTo(writableStream);
console.log("done piping");pipeThrough
pipeThrough(transform) inserts a transform between source and sink, returning the transformed readable. Chain several to build a pipeline.
sourceStream
.pipeThrough(new TextDecoderStream())
.pipeThrough(parser)
.pipeTo(consumer);Highwater Marks
A queuing strategy's highWaterMark sets how many chunks may queue before backpressure kicks in. Tune it to balance throughput and memory.
new ReadableStream(source, new CountQueuingStrategy({ highWaterMark: 4 }));desiredSize
A controller's desiredSize tells the producer how much room is left in the queue. When it hits zero or below, stop enqueuing.
pull(controller) {
while (controller.desiredSize > 0) {
controller.enqueue(nextChunk());
}
}Aborting a Pipe
Pass { signal } to pipeTo to abort the whole pipeline with an AbortController.
const ctrl = new AbortController();
readable.pipeTo(writable, { signal: ctrl.signal });
ctrl.abort();Pipe Options
pipeTo accepts preventClose, preventAbort, and preventCancel to control how errors and completion propagate between ends.
readable.pipeTo(writable, { preventClose: true });A Full Pipeline
Putting it together: fetch bytes, decode, transform, and write to a sink — all with automatic flow control.
const res = await fetch("/data");
await res.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(lineSplitter)
.pipeTo(uiSink);Quick Check
Test backpressure.
Recap: Backpressure and Piping
You met WritableStream and writers, awaited ready for backpressure, connected streams with pipeTo/pipeThrough, tuned highWaterMark and watched desiredSize, and aborted pipelines. That completes the Streams API.
Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Backpressure and Piping” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Backpressure and Piping”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.
What will I learn in “Backpressure and Piping”?
Manage flow control with pipeTo and pipeThrough. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Backpressure and Piping” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Readable Streams
- Streaming fetch Responses
- Transform Streams
- Backpressure and Piping