Skip to content

Commit 48281ec

Browse files
juanarboltargos
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 2d64286 commit 48281ec

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
@@ -401,6 +401,35 @@ This is a destructive and immediate way to destroy a stream. Previous calls to
401401
Use `end()` instead of destroy if data should flush before close, or wait for
402402
the `'drain'` event before destroying the stream.
403403

404+
```cjs
405+
const { Writable } = require('stream');
406+
407+
const myStream = new Writable();
408+
409+
const fooErr = new Error('foo error');
410+
myStream.destroy(fooErr);
411+
myStream.on('error', (fooErr) => console.error(fooErr.message)); // foo error
412+
```
413+
414+
```cjs
415+
const { Writable } = require('stream');
416+
417+
const myStream = new Writable();
418+
419+
myStream.destroy();
420+
myStream.on('error', function wontHappen() {});
421+
```
422+
423+
```cjs
424+
const { Writable } = require('stream');
425+
426+
const myStream = new Writable();
427+
myStream.destroy();
428+
429+
myStream.write('foo', (error) => console.error(error.code));
430+
// ERR_STREAM_DESTROYED
431+
```
432+
404433
Once `destroy()` has been called any further calls will be a no-op and no
405434
further errors except from `_destroy()` may be emitted as `'error'`.
406435

@@ -416,6 +445,16 @@ added: v8.0.0
416445

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

448+
```cjs
449+
const { Writable } = require('stream');
450+
451+
const myStream = new Writable();
452+
453+
console.log(myStream.destroyed); // false
454+
myStream.destroy();
455+
console.log(myStream.destroyed); // true
456+
```
457+
419458
##### `writable.end([chunk[, encoding]][, callback])`
420459
<!-- YAML
421460
added: v0.9.4

0 commit comments

Comments
 (0)