-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathe-try.js
More file actions
67 lines (56 loc) · 1.38 KB
/
e-try.js
File metadata and controls
67 lines (56 loc) · 1.38 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
// Need node.js 23
const sumSync = (a, b) => {
if (typeof a !== 'number') throw Error('Expected a: number');
if (typeof b !== 'number') throw Error('Expected b: number');
return a + b;
};
const sumAsync = async (a, b) => {
if (typeof a !== 'number') throw Error('Expected a: number');
if (typeof b !== 'number') throw Error('Expected b: number');
return a + b;
};
// Call sync, expected 5
Promise.try(sumSync, 2, 3)
.then((result) => {
console.log({ result });
})
.catch((error) => {
console.error({ error: error.message });
})
.finally(() => {
console.log({ finally: '2+3=5' });
});
// Call sync, expected error
Promise.try(sumSync, '4', 5)
.then((result) => {
console.log({ result });
})
.catch((error) => {
console.error({ error: error.message });
})
.finally(() => {
console.log({ finally: 'Wrong type: a' });
});
// Call async, expected 13
Promise.try(sumAsync, 6, 7)
.then((result) => {
console.log({ result });
})
.catch((error) => {
console.error({ error: error.message });
})
.finally(() => {
console.log({ finally: '6+7=13' });
});
// Call async, expected error
Promise.try(sumAsync, 8, '9')
.then((result) => {
console.log({ result });
})
.catch((error) => {
console.error({ error: error.message });
})
.finally(() => {
console.log({ finally: 'Wrong type: b' });
});