Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

fix: Stream.Readable example #367

Merged
merged 2 commits into from
Dec 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/documentation/0052-nodejs-streams/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down