Skip to content

Commit

Permalink
add while loop information
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Jun 4, 2024
1 parent d2eb986 commit 50326e5
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1556,12 +1556,15 @@ readable.on('end', () => {
```

Each call to `readable.read()` returns a chunk of data or `null`, signifying
that there's no more data to read. These chunks aren't automatically concatenated.
that there's no more data to read at that moment. These chunks aren't automatically
concatenated. Because a single `read()` call does not return all the data, using
a while loop may be necessary to continuously read chunks until all data is retrieved.
When reading a large file, `.read()` might return `null` temporarily, indicating
that it has consumed all buffered content but there may be more data yet to be
buffered. In such cases, a new `'readable'` event is emitted once there's more
data in the buffer, and the `'end'` event signifies the end of data transmission.
Typically, you'd use event listeners, such as `'data'` and `'end'`, to handle the
stream's data. When reading a large file, `.read()` might return `null` temporarily,
indicating that it has consumed all buffered content but there may be more data yet to
be buffered. In such cases, a new `'readable'` event is emitted once there's more data
in the buffer, and the `'end'` event signifies the end of data transmission.
stream's data.

Therefore to read a file's whole contents from a `readable`, it is necessary
to collect chunks across multiple `'readable'` events:
Expand Down

0 comments on commit 50326e5

Please sign in to comment.