Skip to content

Commit 55adc5b

Browse files
committed
Intercept unhandled and handled situations
1 parent 976bd89 commit 55adc5b

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

JavaScript/5-warning.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const sum = (a, b) => new Promise((resolve, reject) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
resolve(a + b);
6+
} else {
7+
reject(new Error('a and b should be numbers'));
8+
}
9+
});
10+
11+
process.on('warning', warning => {
12+
console.log({ warning });
13+
});
14+
15+
sum(7, 'A')
16+
.then(data => {
17+
console.log(data);
18+
});

JavaScript/6-intercepted.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const sum = (a, b) => new Promise((resolve, reject) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
resolve(a + b);
6+
} else {
7+
reject(new Error('a and b should be numbers'));
8+
}
9+
});
10+
11+
process.on('unhandledRejection', (reason, promise) => {
12+
console.log({ unhandledRejection: { reason, promise } });
13+
});
14+
15+
sum(7, 'A')
16+
.then(data => {
17+
console.log({ data });
18+
});

JavaScript/7-handled.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const sum = (a, b) => new Promise((resolve, reject) => {
4+
if (typeof a === 'number' && typeof b === 'number') {
5+
resolve(a + b);
6+
} else {
7+
reject(new Error('a and b should be numbers'));
8+
}
9+
});
10+
11+
process.on('unhandledRejection', (reason, promise) => {
12+
console.log({ unhandledRejection: { reason, promise } });
13+
});
14+
15+
process.on('rejectionHandled', promise => {
16+
console.log({ rejectionHandled: { promise } });
17+
});
18+
19+
const p1 = sum(7, 'A')
20+
.then(data => {
21+
console.log(data);
22+
});
23+
24+
//p1.catch(err => {
25+
// console.log({ catch1: err });
26+
//});
27+
28+
setTimeout(() => {
29+
p1.catch(err => {
30+
console.log({ catch2: err });
31+
});
32+
}, 0);

0 commit comments

Comments
 (0)