File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const EventEmitter = require ( 'events' ) ;
3
+
4
+ class WithTime extends EventEmitter {
5
+ execute ( asyncFunc , ...args ) {
6
+ console . time ( 'execute' ) ;
7
+ asyncFunc ( ...args , ( err , data ) => {
8
+ if ( err ) {
9
+ return this . emit ( 'error' , err ) ;
10
+ }
11
+
12
+ this . emit ( 'data' , data ) ;
13
+ console . timeEnd ( 'execute' ) ;
14
+ } ) ;
15
+ }
16
+ }
17
+
18
+ const withTime = new WithTime ( ) ;
19
+
20
+ withTime . on ( 'data' , ( data ) => {
21
+ console . log ( `Length: ${ data . length } ` ) ;
22
+ } ) ;
23
+
24
+ process . once ( 'uncaughtException' , ( err ) => {
25
+ console . log ( err ) ;
26
+ // do some cleanup
27
+ process . exit ( 1 ) ; // exit anyway
28
+ } ) ;
29
+
30
+ withTime . execute ( fs . readFile , '' ) ;
31
+ withTime . execute ( fs . readFile , __filename ) ;
Original file line number Diff line number Diff line change
1
+ const fs = require ( 'fs' ) ;
2
+ const EventEmitter = require ( 'events' ) ;
3
+
4
+ class WithTime extends EventEmitter {
5
+ execute ( asyncFunc , ...args ) {
6
+ console . time ( 'execute' ) ;
7
+ asyncFunc ( ...args , ( err , data ) => {
8
+ if ( err ) {
9
+ return this . emit ( 'error' , err ) ;
10
+ }
11
+
12
+ this . emit ( 'data' , data ) ;
13
+ console . timeEnd ( 'execute' ) ;
14
+ } ) ;
15
+ }
16
+ }
17
+
18
+ const withTime = new WithTime ( ) ;
19
+
20
+ // प्रथम
21
+ withTime . on ( 'data' , ( data ) => {
22
+ console . log ( `Length: ${ data . length } ` ) ;
23
+ } ) ;
24
+
25
+ // दूसरा
26
+ withTime . prependListener ( 'data' , ( data ) => {
27
+ console . log ( `Charaters: ${ data . toString ( ) . length } ` ) ;
28
+ } ) ;
29
+
30
+ // withTime.removeListener ...
31
+
32
+ withTime . execute ( fs . readFile , __filename ) ;
You can’t perform that action at this time.
0 commit comments