liveBook · Manning

WritableStream

As explained in JavaScript in Depth

A stream abstraction that accepts data written by a producer for processing or delivery.

A WritableStream is the destination side of the Web Streams model: producers write chunks to it while an underlying sink processes or delivers them. Unlike Node.js Writable streams, it uses promises rather than a boolean return value and a drain event to signal backpressure. section 10.4 section 10.4.3

The practical failure mode is treating readiness as optional. Obtain a writer, await writer.ready before each write, then close the writer after the final chunk. The promise-based protocol is cleaner than manually coordinating write() results and drain events, but every writer interaction carries promise machinery and therefore affects throughput. section 10.4.3 section 10.3.2

Respecting writable backpressure

const writer = writable.getWriter(); for (const chunk of chunks) { await writer.ready; writer.write(chunk); } await writer.close();

WritableStream also forms the input side of a TransformStream. Backpressure can travel from a full readable queue through the transform to the writable side, stopping transform calls; application code can still bypass that advisory boundary by continuing to push data. Errors surface through rejected promises, so pipeTo() or an awaited read must be observed rather than left unattended. section 10.4.3 section 10.6.1

const writer = writable.getWriter();
for (const chunk of chunks) {
  await writer.ready;  // #A
  writer.write(chunk);
}
await writer.close();

Inspect the minimal writer loop that makes readiness, chunk delivery, and orderly closure explicit. section 10.4.3

How JavaScript in Depth covers it

How deep it goes
Explains writer promises, backpressure propagation, buffering, and error rejection, but stops short of underlying sink construction.
Examples
Uses a writer loop with writer.ready, write(), and close(), plus TransformStream buffering and pipeline comparisons.
What it assumes
Assumes familiarity with promises and streaming, while contrasting Node.js event-based backpressure with Web Streams from the underlying roles.
What you can do after
Operate/configure it by awaiting writer readiness, writing chunks, closing cleanly, and observing rejected pipeline promises.
How it connects
Connects WritableStream to ReadableStream, TransformStream, backpressure, pipeTo(), promise rejection, and Node.js streams.

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.