Skip to content

Commit

Permalink
doc: add code examples to Writable.destroy() and Writable.destroyed
Browse files Browse the repository at this point in the history
PR-URL: #39491
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
juanarbol authored and danielleadams committed Aug 16, 2021
1 parent b37575b commit 8072517
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,35 @@ This is a destructive and immediate way to destroy a stream. Previous calls to
Use `end()` instead of destroy if data should flush before close, or wait for
the `'drain'` event before destroying the stream.

```cjs
const { Writable } = require('stream');

const myStream = new Writable();

const fooErr = new Error('foo error');
myStream.destroy(fooErr);
myStream.on('error', (fooErr) => console.error(fooErr.message)); // foo error
```

```cjs
const { Writable } = require('stream');

const myStream = new Writable();

myStream.destroy();
myStream.on('error', function wontHappen() {});
```

```cjs
const { Writable } = require('stream');

const myStream = new Writable();
myStream.destroy();

myStream.write('foo', (error) => console.error(error.code));
// ERR_STREAM_DESTROYED
```

Once `destroy()` has been called any further calls will be a no-op and no
further errors except from `_destroy()` may be emitted as `'error'`.

Expand All @@ -426,6 +455,16 @@ added: v8.0.0

Is `true` after [`writable.destroy()`][writable-destroy] has been called.

```cjs
const { Writable } = require('stream');

const myStream = new Writable();

console.log(myStream.destroyed); // false
myStream.destroy();
console.log(myStream.destroyed); // true
```

##### `writable.end([chunk[, encoding]][, callback])`
<!-- YAML
added: v0.9.4
Expand Down

0 comments on commit 8072517

Please sign in to comment.