liveBook · Manning

Write operation

As explained in JavaScript in Depth

A stream operation that writes a data chunk to a Node.js writable stream and returns an advisory indication of buffering capacity.

A Writable stream’s write() returns an advisory boolean: true indicates room remains; false means its buffer has exceeded highWaterMark. Keep writing after false, and chunks accumulate, increasing memory use and garbage-collection costs; the signal does not enforce a limit. section 10.3.1

const writable = getWritableSomehow();
if (!writable.write('something')) {
  // Buffer is full, stop writing until drain
  writable.on('drain', () => {
    // Safe to resume writing
  });
}

Check write()’s return value and resume writing only after the Writable emits 'drain'. section 10.3.1

How JavaScript in Depth covers it

How deep it goes
Explains the boolean signal, highWaterMark, drain event, and consequences of ignoring backpressure.
Examples
Uses a conditional write followed by a 'drain' listener before writing resumes.
What you can do after
Apply it via Node.js Writable streams by stopping writes after false and resuming on 'drain'.
How it connects
Connects write() to Writable buffers, highWaterMark, the 'drain' event, and backpressure.

Also explained in

Compare how 3 books explain write operation →

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.