-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathb-errors.js
More file actions
44 lines (38 loc) · 708 Bytes
/
Copy pathb-errors.js
File metadata and controls
44 lines (38 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
// Implementation
const adder = (value) => {
const add = (a) => {
value += a;
if (value >= add.maxValue) {
setImmediate(() => {
const err = new Error('max value reached');
add.maxEvent(err, value);
});
}
return add;
};
// callback-last
add.max = (max, event) => {
add.maxValue = max;
add.maxEvent = event;
return add;
};
return add;
};
// Usage
// error-first
const maxReached = (err, value) => {
if (err) throw err;
console.log({ value });
};
try {
const a1 = adder(10).max(100, maxReached)(-5);
a1(25);
a1(50);
a1(75);
a1(100);
a1(-200)(50)(30);
} catch {
console.log('Never');
}
console.log('end');