Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 94fe3a3

Browse filesBrowse files
committed
Add error handling example
1 parent 003360b commit 94fe3a3
Copy full SHA for 94fe3a3

File tree

Expand file treeCollapse file tree

1 file changed

+45
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+45
-0
lines changed

‎JavaScript/b-errors.js

Copy file name to clipboard
+45Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.