-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path5-promise.js
More file actions
42 lines (36 loc) · 858 Bytes
/
5-promise.js
File metadata and controls
42 lines (36 loc) · 858 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
'use strict';
const sum = (a, b) => new Promise((totalolve, reject) => {
if (typeof a === 'number' && typeof b === 'number') {
totalolve(a + b);
} else {
reject(new Error('a and b should be numbers'));
}
});
const x = 2;
const y = 3;
sum(x, y)
.then((total) => {
console.log({ x, y, total });
})
.catch((err) => {
console.error({ x, y, err });
});
const z = 7;
const c = 'A';
sum(z, c)
.then((res) => {
console.log({ z, c, res });
})
.catch((err) => {
console.log({ z, c, err });
});
/*
sum(z, c)
.then((res) => {
console.log({ z, c, res });
});
UnhandledPromiseRejectionWarning: Error: a and b should be numbers
DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
*/