From 6e8fa7d1b232e791e6964cd98d94069055c15be5 Mon Sep 17 00:00:00 2001 From: shisama Date: Mon, 11 Nov 2019 00:31:32 +0900 Subject: [PATCH 1/2] fix: Stream.Readable example --- .../0052-nodejs-streams/index.md | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/documentation/0052-nodejs-streams/index.md b/src/documentation/0052-nodejs-streams/index.md index c77f24aacc..a1c0236786 100644 --- a/src/documentation/0052-nodejs-streams/index.md +++ b/src/documentation/0052-nodejs-streams/index.md @@ -112,13 +112,29 @@ There are four classes of streams: ## How to create a readable stream -We get the Readable stream from the [`stream` module](https://nodejs.org/api/stream.html), and we initialize it +We get the Readable stream from the [`stream` module](https://nodejs.org/api/stream.html), and we initialize it and implement the readable._read() method. + +First create a stream object: ```js const Stream = require('stream') const readableStream = new Stream.Readable() ``` +then implement `_read`: + +```js +readableStream._read = () => {} +``` + +You can also implement `_read` using the `read` option: + +```js +const readableStream = new Stream.Readable({ + read() {} +}) +``` + Now that the stream is initialized, we can send data to it: ```js @@ -160,7 +176,9 @@ How do we read data from a readable stream? Using a writable stream: ```js const Stream = require('stream') -const readableStream = new Stream.Readable() +const readableStream = new Stream.Readable({ + read() {} +}) const writableStream = new Stream.Writable() writableStream._write = (chunk, encoding, next) => { @@ -197,7 +215,9 @@ Use the `end()` method: ```js const Stream = require('stream') -const readableStream = new Stream.Readable() +const readableStream = new Stream.Readable({ + read() {} +}) const writableStream = new Stream.Writable() writableStream._write = (chunk, encoding, next) => { From f489be402a6446fdc12abf60a3d148fd2e6549f8 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Tue, 12 Nov 2019 01:35:45 +0900 Subject: [PATCH 2/2] fix: escape the underscore for `readable._read()` Co-Authored-By: Nick Schonning --- src/documentation/0052-nodejs-streams/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/documentation/0052-nodejs-streams/index.md b/src/documentation/0052-nodejs-streams/index.md index a1c0236786..49981607c7 100644 --- a/src/documentation/0052-nodejs-streams/index.md +++ b/src/documentation/0052-nodejs-streams/index.md @@ -112,7 +112,7 @@ There are four classes of streams: ## How to create a readable stream -We get the Readable stream from the [`stream` module](https://nodejs.org/api/stream.html), and we initialize it and implement the readable._read() method. +We get the Readable stream from the [`stream` module](https://nodejs.org/api/stream.html), and we initialize it and implement the `readable._read()` method. First create a stream object: