File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ // Implementation
4
+
5
+ const adder = ( value ) => {
6
+ const add = ( a ) => {
7
+ value += a ;
8
+ if ( value >= add . maxValue ) {
9
+ setImmediate ( ( ) => {
10
+ add . maxEvent ( new Error ( 'max value reached' ) , value ) ;
11
+ } ) ;
12
+ }
13
+ return add ;
14
+ } ;
15
+ // callback-last
16
+ add . max = ( max , event ) => {
17
+ add . maxValue = max ;
18
+ add . maxEvent = event ;
19
+ return add ;
20
+ } ;
21
+ return add ;
22
+ } ;
23
+
24
+ // Usage
25
+
26
+ // error-first
27
+ const maxReached = ( err , value ) => {
28
+ if ( err ) {
29
+ console . log ( 'value: ' + value ) ;
30
+ throw err ;
31
+ }
32
+ } ;
33
+
34
+ try {
35
+ const a1 = adder ( 10 ) . max ( 100 , maxReached ) ( - 5 ) ;
36
+ a1 ( 25 ) ;
37
+ a1 ( 50 ) ;
38
+ a1 ( 75 ) ;
39
+ a1 ( 100 ) ;
40
+ a1 ( - 200 ) ( 50 ) ( 30 ) ;
41
+ } catch ( e ) {
42
+ console . log ( 'Never' ) ;
43
+ }
44
+
45
+ console . log ( 'end' ) ;
You can’t perform that action at this time.
0 commit comments