Backpressure and Piping
Manage flow control with pipeTo and pipeThrough.
Backpressure and Piping is a free JavaScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Backpressure and Piping” lesson free?
Yes — the full text of “Backpressure and Piping” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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