-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (68 loc) · 1.62 KB
/
index.js
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
72
73
74
75
const ONE_SECOND = 1000;
const TWO_SECONDS = 2000;
const THREE_SECONDS = 3000;
const CREATE_ERROR = process.env.CREATE_ERROR == "true";
const delayLog1 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("1 - after three seconds");
resolve();
}, THREE_SECONDS);
});
};
const delayLog2 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("2 - after two seconds");
if (CREATE_ERROR) {
reject(Error("arbitrary issue"));
} else {
resolve();
}
}, TWO_SECONDS);
});
};
const delayLog3 = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("3 - after one seconds");
resolve();
}, ONE_SECOND);
});
};
const invokeWithoutAsyncAwait = () => {
delayLog1().then(
(result) => {
delayLog2().then(
(result) => {
delayLog3().then(
(result) => {
console.log("Finished invoking without async/await.");
},
(error) => {
console.log("--- ERROR in PROMISE: 3 ---");
}
);
},
(error) => {
console.log("--- ERROR in PROMISE: 2 ---");
}
);
},
(error) => {
console.log("--- ERROR in PROMISE: 1 ---");
}
);
};
const invokeWithAsyncAwait = async () => {
try {
await delayLog1();
await delayLog2();
await delayLog3();
console.log("Finished invoking with async/await.");
} catch (err) {
console.log("--- ERROR in ASYNC/AWAIT ---");
}
};
invokeWithoutAsyncAwait();
invokeWithAsyncAwait();