File tree 3 files changed +78
-0
lines changed
3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ const c1 = next => {
2
+ console . log ( '[c1-1]' ) ;
3
+ next ( ) ;
4
+ console . log ( '[c1-2]' ) ;
5
+ }
6
+
7
+ const c2 = next => {
8
+ console . log ( '[c2-1]' ) ;
9
+ next ( ) ;
10
+ console . log ( '[c2-2]' ) ;
11
+ }
12
+
13
+ const runCallbacks = cbs => {
14
+ if ( Array . isArray ( cbs ) ) {
15
+ let idx = 0 ;
16
+ const next = ( ) => {
17
+ const cb = cbs [ idx ++ ] ;
18
+ typeof cb === 'function' && cb . call ( cb , next ) ;
19
+ }
20
+ next ( ) ;
21
+ }
22
+ }
23
+
24
+ runCallbacks ( [ c1 , c2 ] ) ;
25
+ /**
26
+ 输出结果:
27
+ [c1-1]
28
+ [c1-2]
29
+ [c2-1]
30
+ [c2-2]
31
+ */
Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+
3
+ var c1 = function c1 ( next ) {
4
+ console . log ( '[c1-1]' ) ;
5
+ next ( ) ;
6
+ console . log ( '[c1-2]' ) ;
7
+ } ;
8
+
9
+ var c2 = function c2 ( next ) {
10
+ console . log ( '[c2-1]' ) ;
11
+ next ( ) ;
12
+ console . log ( '[c2-2]' ) ;
13
+ } ;
14
+
15
+ var runCallbacks = function runCallbacks ( cbs ) {
16
+ if ( Array . isArray ( cbs ) ) {
17
+ var idx = 0 ;
18
+
19
+ var next = function next ( ) {
20
+ var cb = cbs [ idx ++ ] ;
21
+ typeof cb === 'function' && cb . call ( cb , next ) ;
22
+ } ;
23
+
24
+ next ( ) ;
25
+ }
26
+ } ;
27
+
28
+ runCallbacks ( [ c1 , c2 ] ) ;
29
+ /**
30
+ 输出结果:
31
+ [c1-1]
32
+ [c1-2]
33
+ [c2-1]
34
+ [c2-2]
35
+ */
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " Learn" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " index.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords" : [],
10
+ "author" : " " ,
11
+ "license" : " ISC"
12
+ }
You can’t perform that action at this time.
0 commit comments