Skip to content

Commit 881d9be

Browse files
committed
doc: readable event clarification
per nodejs/node-v0.x-archive#14597 Indicate that `'readable'` indicates only that data can be read from the stream, not that there is actually data to be consumed. `readable.read([size])` can still return null. Includes an example that illustrates the point. Reviewed-By: James M Snell <[email protected]> PR-URL: nodejs/node-v0.x-archive#25591
1 parent b6378f0 commit 881d9be

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/api/stream.markdown

+26
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,32 @@ again when more data is available.
167167
The `readable` event is not emitted in the "flowing" mode with the
168168
sole exception of the last one, on end-of-stream.
169169

170+
Note that the `'readable'` event indicates only that data *can* be
171+
read from the stream. It does not indicate whether there is actual
172+
data to be consumed. The callback passed to handle the `'readable'`
173+
event must be prepared to handle a `null` response from
174+
`readable.read([size])`. For instance, in the following example, `foo.txt`
175+
is an empty file:
176+
177+
```javascript
178+
var fs = require('fs');
179+
var rr = fs.createReadStream('foo.txt');
180+
rr.on('readable', function() {
181+
console.log('readable:', rr.read());
182+
});
183+
rr.on('end', function() {
184+
console.log('end');
185+
});
186+
```
187+
188+
The output of running this script is:
189+
190+
```
191+
bash-3.2$ node test.js
192+
readable: null
193+
end
194+
```
195+
170196
#### Event: 'data'
171197

172198
* `chunk` {Buffer | String} The chunk of data.

0 commit comments

Comments
 (0)