Skip to content

Commit 94fe3a3

Browse files
committed
Add error handling example
1 parent 003360b commit 94fe3a3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

JavaScript/b-errors.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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');

0 commit comments

Comments
 (0)