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
|
const printResult = (result: number) => {
console.log(result);
};
const printOddEven = (value: number) => {
value % 2 === 0
? console.log("Result is even")
: console.log("Result is odd");
};
const divide = (a: number, b: number): Promise<number> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (b === 0) reject(new Error("Division Error!"));
resolve(a / b);
}, 1000);
});
};
const addOne = (a: number): Promise<number> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(a + 1);
}, 1000);
});
};
const calculate = async (a: number, b: number): Promise<number> => {
const division = await divide(a, b);
const result = await addOne(division);
return result;
};
// usage
calculate(4, 2)
.then((result) => {
printResult(result);
printOddEven(result);
})
.catch((err) => console.log(err.message))
.finally(() => console.log("Operation Complete."));
|