Skip to content

Commit eda48ee

Browse files
committed
readable事件例子
1 parent 81d0db1 commit eda48ee

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var fs = require('fs');
2+
var readstream = fs.createReadStream('./hello.txt');
3+
readstream.on('readable', function() {
4+
console.log('readable: %s', readstream.read());
5+
});
6+
readstream.on('end', function() {
7+
console.log('end');
8+
});
9+
10+
// 输出:
11+
// readable: hello world
12+
// readable: null
13+
// end

模块/stream.readable.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,23 @@ events.js:160
146146
Error: ENOENT: no such file or directory, open './none-exists.txt'
147147
at Error (native)
148148
```
149+
150+
## readable事件
151+
152+
1. 触发时机:当有数据可读时,或者数据已经读完,但是end尚未触发(感觉这样设计不合理)
153+
154+
```javascript
155+
var fs = require('fs');
156+
var readstream = fs.createReadStream('./hello.txt');
157+
readstream.on('readable', function() {
158+
console.log('readable: %s', readstream.read());
159+
});
160+
readstream.on('end', function() {
161+
console.log('end');
162+
});
163+
164+
// 输出:
165+
// readable: hello world
166+
// readable: null
167+
// end
168+
```

0 commit comments

Comments
 (0)