Skip to content

Commit 89adc16

Browse files
juanarboljasnell
authored andcommitted
doc: add code examples to Writable.destroy() and Writable.destroyed
PR-URL: #39491 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a27d245 commit 89adc16

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

doc/api/stream.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,35 @@ This is a destructive and immediate way to destroy a stream. Previous calls to
411411
Use `end()` instead of destroy if data should flush before close, or wait for
412412
the `'drain'` event before destroying the stream.
413413

414+
```cjs
415+
const { Writable } = require('stream');
416+
417+
const myStream = new Writable();
418+
419+
const fooErr = new Error('foo error');
420+
myStream.destroy(fooErr);
421+
myStream.on('error', (fooErr) => console.error(fooErr.message)); // foo error
422+
```
423+
424+
```cjs
425+
const { Writable } = require('stream');
426+
427+
const myStream = new Writable();
428+
429+
myStream.destroy();
430+
myStream.on('error', function wontHappen() {});
431+
```
432+
433+
```cjs
434+
const { Writable } = require('stream');
435+
436+
const myStream = new Writable();
437+
myStream.destroy();
438+
439+
myStream.write('foo', (error) => console.error(error.code));
440+
// ERR_STREAM_DESTROYED
441+
```
442+
414443
Once `destroy()` has been called any further calls will be a no-op and no
415444
further errors except from `_destroy()` may be emitted as `'error'`.
416445

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

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

458+
```cjs
459+
const { Writable } = require('stream');
460+
461+
const myStream = new Writable();
462+
463+
console.log(myStream.destroyed); // false
464+
myStream.destroy();
465+
console.log(myStream.destroyed); // true
466+
```
467+
429468
##### `writable.end([chunk[, encoding]][, callback])`
430469
<!-- YAML
431470
added: v0.9.4

0 commit comments

Comments
 (0)