# Adding Error Management to our own Async-Await
Add error management to the home-made Async-Await implementation you did in the previous lab so that a program like this:
import { awaitFor, waiter } from './async-await.mjs';
function doTask1(arg) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(arg), 100)
})
}
function doTask2(arg) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(arg + 2), 100)
})
}
function doTask3(arg) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(arg + 3), 100)
})
}
function doTaskErr(arg) {
return new Promise((resolve, reject) => {
setTimeout(() => reject("Ay!!!!!!!!", 100))
})
}
function* init(arg) {
const res1 = yield doTask1(arg);
console.log(res1);
const res2 = yield doTask2(res1);
console.log(res2);
const res3 = yield doTask3(res2);
console.log(res3);
return res3;
}
function* fails(arg) {
try {
console.log("Error handling example");
const res1 = awaitFor(yield doTask1(arg));
console.log(res1);
const res2 = awaitFor(yield doTaskErr(res1));
console.log(res2);
const res3 = awaitFor(yield doTask3(res2));
console.log(res3);
return res3;
} catch (err) {
console.log(`Inside "fails" catch: ${err}`);
}
}
function* main() {
try {
const res = awaitFor(yield waiter(init, 3)());
console.log(`res=${res}`);
awaitFor(yield waiter(fails, 3)());
console.log(`Executed since the error was catched`);
} catch (err) {
console.log(`Inside "main" catch: ${err}`);
}
}
waiter(main)();
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
68
69
70
71
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
68
69
70
71
should produce an output like this:
➜ async-await-equal-generators-plus-promises git:(trycatch) node solution.mjs
3
5
8
res=8
Error handling example
3
Inside "fails" catch: Ay!!!!!!!!
Executed since the error was catched
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# Delivery
Use the repository created for the previous lab.
Create a branch called trycatch
and develop your solution to this lab in it.
# See
Last Updated: 2 months ago