-
Notifications
You must be signed in to change notification settings - Fork 2
/
promises.js
60 lines (56 loc) · 1.5 KB
/
promises.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
function waitAndPrint(message, callback) {
// setTimeout(function () {});
setTimeout(() => {
if (message === "Error") {
callback(`We didn't like this message: "${message}"`);
} else {
console.log(message);
callback(null);
}
}, 1000);
}
// waitAndPrint("3", (err1) => {
// if (err1) {
// console.log("== Error:", err1);
// } else {
// waitAndPrint("2", (err2) => {
// if (err2) {
// console.log("== Error:", err2);
// } else {
// waitAndPrint("1", (err3) => {
// if (err3) {
// console.log("== Error:", err3);
// }
// });
// }
// });
// }
// });
// let p = new Promise((resolve, reject) => {
// doAsyncOperation((err, results) => {
// if (err) {
// reject(err);
// } else {
// resolve(resolve);
// }
// });
// });
function waitAndPrintWithPromises(message) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (message === "Error") {
reject(`We didn't like this message: "${message}"`);
} else {
console.log(message);
resolve();
}
}, 1000);
});
}
waitAndPrintWithPromises("3")
.then(() => { return waitAndPrintWithPromises("2"); })
.then(() => { return waitAndPrintWithPromises("Error"); })
// .catch((err) => { console.log("== Error (different):", err); })
.then(() => { return waitAndPrintWithPromises("... Blastoff!"); })
.catch((err) => { console.log("== Error:", err); });
console.log("ok");