File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -504,6 +504,38 @@ readable.on('data', (data) =>
504504);
505505```
506506
507+ Here's an example of using [ ` .push() ` ] [ ] with a Readable stream.
508+
509+ ``` javascript
510+ const { Readable } = require (' stream' );
511+
512+ // Create a custom Readable stream
513+ const myReadableStream = new Readable ({
514+ objectMode: true ,
515+ read (size ) {
516+ // Push some data onto the stream
517+ this .push ({ message: ' Hello, world!' });
518+ this .push (null ); // Mark the end of the stream
519+ }
520+ });
521+
522+ // Consume the stream
523+ myReadableStream .on (' data' , (chunk ) => {
524+ console .log (chunk);
525+ });
526+
527+ // Output:
528+ // { message: 'Hello, world!' }
529+ ```
530+ In this example, we create a custom Readable stream that pushes a single object
531+ onto the stream using [ ` .push() ` ] [ ] . The [ ` ._read() ` ] [ ] method is called when the stream is ready
532+ to consume data, and in this case, we immediately push some data onto the stream and
533+ mark the end of the stream by pushing null.
534+
535+ We then consume the stream by listening for the 'data' event and logging each chunk of
536+ data that is pushed onto the stream. In this case, we only push a single chunk of data
537+ onto the stream, so we only see one log message.
538+
507539## Rules specific to Writable Streams
508540
509541Recall that a [ ` .write() ` ] [ ] may return true or false dependent on some
You can’t perform that action at this time.
0 commit comments