Skip to content

Commit a297d85

Browse files
0hmXavivkeller
andauthored
docs: add example of respecting backpressure in Readable stream (#7854)
* docs: add example of respecting backpressure in Readable stream * Update apps/site/pages/en/learn/modules/backpressuring-in-streams.md Co-authored-by: Aviv Keller <[email protected]> Signed-off-by: 0hm☘️ <[email protected]> * fix: correct indentation in MyReadable class example --------- Signed-off-by: 0hm☘️ <[email protected]> Co-authored-by: Aviv Keller <[email protected]>
1 parent f93bd4b commit a297d85

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

apps/site/pages/en/learn/modules/backpressuring-in-streams.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,20 @@ class MyReadable extends Readable {
557557
}
558558
```
559559

560+
Here is an example of good practice, where the `Readable` stream respects backpressure by checking the return value of `this.push()`:
561+
562+
```js
563+
class MyReadable extends Readable {
564+
_read(size) {
565+
let chunk;
566+
let canPushMore = true;
567+
while (canPushMore && null !== (chunk = getNextChunk())) {
568+
canPushMore = this.push(chunk);
569+
}
570+
}
571+
}
572+
```
573+
560574
Additionally, from outside the custom stream, there are pitfalls to ignoring
561575
backpressure. In this counter-example of good practice, the application's code
562576
forces data through whenever it is available (signaled by the

0 commit comments

Comments
 (0)