title: Node.js Writable stream: write vs _write
body: <p>I'm reading official Node.js docs to understand streams. I'm implementing Writable stream, but I can't understand what is the difference between <code>write</code> and <code>_write</code>.</p>
  
  <p>Quoting the docs from <a href="https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback_1" rel="nofollow noreferrer">this</a> section:</p>
  
  <blockquote>
    <p>All calls to writable.write() that occur between the time
    writable._write() is called and the callback is called will cause the
    written data to be buffered. When the callback is invoked, the stream
    might emit a 'drain' event. If a stream implementation is capable of
    processing multiple chunks of data at once, the writable._writev()
    method should be implemented.</p>
  </blockquote>
  
  <p>this only gives me the idea that both behave differently, but I can't understand how.</p>
  
  <p>May be by taking an example based on <a href="https://nodejs.org/api/stream.html#stream_errors_while_writing" rel="nofollow noreferrer">here</a> in the docs, can anyone explain what is the difference in the ways the code snippets given below will behave on receiving data from <code>Readable/Transform</code> stream? What are <code>write</code> and <code>_write</code> counterparts in <code>Readable</code> stream, if any?</p>
  
  <pre><code>const { Writable } = require('stream');
  
  const myWritable = new Writable({
    write(chunk, encoding, callback) {
      if (chunk.toString().indexOf('a') &gt;= 0) {
        callback(new Error('chunk is invalid'));
      } else {
        callback();
      }
    }
  });
  </code></pre>
  
  <pre><code>const { Writable } = require('stream');
  
  class MyWritable extends Writable {
    _write(chunk, encoding, callback) {
      if (chunk.toString().indexOf('a') &gt;= 0) {
        callback(new Error('chunk is invalid'));
      } else {
        callback();
      }
    }
  }
  </code></pre>