diff --git a/doc/api/stream.md b/doc/api/stream.md index eed68fcc54ea31..10c68d9b4dd074 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1953,56 +1953,6 @@ const myWritable = new Writable({ }); ``` -#### `writable._construct(callback)` - - -* `callback` {Function} Call this function (optionally with an error - argument) when the stream has finished initializing. - -The `_construct()` method MUST NOT be called directly. It may be implemented -by child classes, and if so, will be called by the internal `Writable` -class methods only. - -This optional function will be called in a tick after the stream constructor -has returned, delaying any `_write()`, `_final()` and `_destroy()` calls until -`callback` is called. This is useful to initialize state or asynchronously -initialize resources before the stream can be used. - -```js -const { Writable } = require('stream'); -const fs = require('fs'); - -class WriteStream extends Writable { - constructor(filename) { - super(); - this.filename = filename; - this.fd = fd; - } - _construct(callback) { - fs.open(this.filename, (fd, err) => { - if (err) { - callback(err); - } else { - this.fd = fd; - callback(); - } - }); - } - _write(chunk, encoding, callback) { - fs.write(this.fd, chunk, callback); - } - _destroy(err, callback) { - if (this.fd) { - fs.close(this.fd, (er) => callback(er || err)); - } else { - callback(err); - } - } -} -``` - #### `writable._write(chunk, encoding, callback)` - -* `callback` {Function} Call this function (optionally with an error - argument) when the stream has finished initializing. - -The `_construct()` method MUST NOT be called directly. It may be implemented -by child classes, and if so, will be called by the internal `Readable` -class methods only. - -This optional function will be scheduled in the next tick by the stream -constructor, delaying any `_read()` and `_destroy()` calls until `callback` is -called. This is useful to initialize state or asynchronously initialize -resources before the stream can be used. - -```js -const { Readable } = require('stream'); -const fs = require('fs'); - -class ReadStream extends Readable { - constructor(filename) { - super(); - this.filename = filename; - this.fd = null; - } - _construct(callback) { - fs.open(this.filename, (fd, err) => { - if (err) { - callback(err); - } else { - this.fd = fd; - callback(); - } - }); - } - _read(n) { - const buf = Buffer.alloc(n); - fs.read(this.fd, buf, 0, n, null, (err, bytesRead) => { - if (err) { - this.destroy(err); - } else { - this.push(bytesRead > 0 ? buf.slice(0, bytesRead) : null); - } - }); - } - _destroy(err, callback) { - if (this.fd) { - fs.close(this.fd, (er) => callback(er || err)); - } else { - callback(err); - } - } -} -``` - #### `readable._read(size)`