From d4146fee51353136a7109a5b2fb6a5fd18f51116 Mon Sep 17 00:00:00 2001 From: Rishabh Bhandari Date: Fri, 24 Feb 2023 01:46:49 +0530 Subject: [PATCH 1/2] added example for back-pressure Signed-off-by: Rishabh Bhandari --- .../docs/guides/backpressuring-in-streams.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/locale/en/docs/guides/backpressuring-in-streams.md b/locale/en/docs/guides/backpressuring-in-streams.md index 7358f3894c724..2c17f5f8607ba 100644 --- a/locale/en/docs/guides/backpressuring-in-streams.md +++ b/locale/en/docs/guides/backpressuring-in-streams.md @@ -504,6 +504,40 @@ readable.on('data', (data) => ); ``` +Here's an example of using [`.push()`][] with a Readable stream. + +```javascript +const { Readable } = require('stream'); + +// Create a custom Readable stream +const myReadableStream = new Readable({ + objectMode: true, + read(size) { + // Push some data onto the stream + this.push({ message: 'Hello, world!' }); + this.push(null); // Mark the end of the stream + } +}); + +// Consume the stream +myReadableStream.on('data', (chunk) => { + console.log(chunk); +}); + +// Output: +// { message: 'Hello, world!' } +``` +In this example, we create a custom Readable stream that pushes a single object +onto the stream using [`.push()`][]. The [`._read()`][] method is called when the stream is ready +to consume data, and in this case, we immediately push some data onto the stream and +mark the end of the stream by pushing null. + +We then consume the stream by listening for the 'data' event and logging each chunk of +data that is pushed onto the stream. In this case, we only push a single chunk of data +onto the stream, so we only see one log message. + + + ## Rules specific to Writable Streams Recall that a [`.write()`][] may return true or false dependent on some From ed48a557af774ecf01ecb9c28532b3a26d22926c Mon Sep 17 00:00:00 2001 From: Rishabh Bhandari Date: Mon, 27 Feb 2023 20:46:36 +0530 Subject: [PATCH 2/2] linting fixes Signed-off-by: Rishabh Bhandari --- locale/en/docs/guides/backpressuring-in-streams.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/locale/en/docs/guides/backpressuring-in-streams.md b/locale/en/docs/guides/backpressuring-in-streams.md index 2c17f5f8607ba..89d3270d408e4 100644 --- a/locale/en/docs/guides/backpressuring-in-streams.md +++ b/locale/en/docs/guides/backpressuring-in-streams.md @@ -528,7 +528,7 @@ myReadableStream.on('data', (chunk) => { // { message: 'Hello, world!' } ``` In this example, we create a custom Readable stream that pushes a single object -onto the stream using [`.push()`][]. The [`._read()`][] method is called when the stream is ready +onto the stream using [`.push()`][]. The [`._read()`][] method is called when the stream is ready to consume data, and in this case, we immediately push some data onto the stream and mark the end of the stream by pushing null. @@ -536,8 +536,6 @@ We then consume the stream by listening for the 'data' event and logging each ch data that is pushed onto the stream. In this case, we only push a single chunk of data onto the stream, so we only see one log message. - - ## Rules specific to Writable Streams Recall that a [`.write()`][] may return true or false dependent on some