Skip to content

Commit cc492c3

Browse files
mcollinaMylesBorins
authored andcommitted
doc: resume a stream after pipe() and unpipe()
Clarifies the behavior of streams when _readableState.flowing is false. resume() must be called explicitly for the 'data' event to be emitted again. Fixes: #1041 PR-URL: #13329 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent ae00f25 commit cc492c3

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

doc/api/stream.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -566,17 +566,31 @@ possible states:
566566
* `readable._readableState.flowing = true`
567567

568568
When `readable._readableState.flowing` is `null`, no mechanism for consuming the
569-
streams data is provided so the stream will not generate its data.
570-
571-
Attaching a listener for the `'data'` event, calling the `readable.pipe()`
569+
streams data is provided so the stream will not generate its data. While in this
570+
state, attaching a listener for the `'data'` event, calling the `readable.pipe()`
572571
method, or calling the `readable.resume()` method will switch
573572
`readable._readableState.flowing` to `true`, causing the Readable to begin
574573
actively emitting events as data is generated.
575574

576575
Calling `readable.pause()`, `readable.unpipe()`, or receiving "back pressure"
577576
will cause the `readable._readableState.flowing` to be set as `false`,
578577
temporarily halting the flowing of events but *not* halting the generation of
579-
data.
578+
data. While in this state, attaching a listener for the `'data'` event
579+
would not cause `readable._readableState.flowing` to switch to `true`.
580+
581+
```js
582+
const { PassThrough, Writable } = require('stream');
583+
const pass = new PassThrough();
584+
const writable = new Writable();
585+
586+
pass.pipe(writable);
587+
pass.unpipe(writable);
588+
// flowing is now false
589+
590+
pass.on('data', (chunk) => { console.log(chunk.toString()); });
591+
pass.write('ok'); // will not emit 'data'
592+
pass.resume(); // must be called to make 'data' being emitted
593+
```
580594

581595
While `readable._readableState.flowing` is `false`, data may be accumulating
582596
within the streams internal buffer.

0 commit comments

Comments
 (0)