File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
examples/2017.05.23-node-stream-readable/event Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -146,3 +146,23 @@ events.js:160
146
146
Error: ENOENT: no such file or directory, open ' ./none-exists.txt'
147
147
at Error (native)
148
148
```
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
+ ```
You can’t perform that action at this time.
0 commit comments