Course contentsShow
← JavaScript
Lesson 1577 of 2,72334. Streams and BuffersFree lesson

The write() Return Value

Understanding how writable.write() returns false when the internal buffer is full, signaling backpressure.

The write() Return Value

You'll learn how to interpret the boolean returned by writable.write() to detect when a stream's buffer is full.


What Does write() Return?

When you call writable.write(chunk) on a writable stream, it doesn't just accept your data silently—it gives you feedback. The method returns a boolean value:

  • true: The data was successfully written to the internal buffer, and there's still room for more. Keep writing if you want!
  • false: The internal buffer is full or exceeds the high water mark. This is your signal to stop writing and wait.

Why This Matters

Think of a stream's internal buffer like a waiting room with limited seats. When write() returns false, it's like the receptionist saying "Sorry, we're at capacity—please wait outside until we call you." This is the stream's way of telling you it's experiencing backpressure.

Ignoring a false return value won't crash your program, but it will cause data to pile up in memory. If you keep writing regardless, you risk memory bloat and performance issues.

Reading the Signal

const keepGoing = writableStream.write(chunk);

if (!keepGoing) {
  // Buffer is full - stop writing and wait for 'drain'
  console.log('Backpressure detected!');
}

When write() returns false, you should pause your writing operations and listen for the 'drain' event before continuing. This cooperative pattern keeps memory usage stable and your stream healthy.

Key Takeaway: writable.write() returns false when the internal buffer is full, signaling backpressure—your cue to pause writing until the 'drain' event fires.