Skip to content

Commit b176a76

Browse files
committed
doc: add code examples to Writable.destroy() and Writable.destroyed
1 parent 3cbaabc commit b176a76

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+
422+
myStream.on('error', (fooErr) => console.error(fooErr.message)); // foo error
423+
```
424+
425+
```cjs
426+
const { Writable } = require('stream');
427+
428+
const myStream = new Writable();
429+
430+
myStream.destroy();
431+
myStream.on('error', function wontHappen() {});
432+
```
433+
434+
```cjs
435+
const { Writable } = require('stream');
436+
437+
const myStream = new Writable();
438+
myStream.destroy();
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)