-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
74 lines (67 loc) · 1.63 KB
/
promise.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
const boilWater = () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log("Boiling water...");
resolve();
}, 5000);
});
};
const grindingCoffeeBean = () => {
return new Promise((resolve,reject) => {
setTimeout(function () {
console.log("Beans is ready")
resolve();
}, 2000);
});
};
const addSugar = () => {
return new Promise((resolve,reject) => {
setTimeout(() => {
console.log("Adding sugar");
resolve();
// reject("No sugar found");
}, 2000);
});
};
const mixAllIngredients = () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log("Mixing All");
resolve();
}, 2000);
});
};
const pourOut = () => {
console.log("Everything done");
};
// Handling concurrent Promises
// boilWater()
// .then(grindingCoffeeBean)
// .then(addSugar)
// .then(mixAllIngredients)
// .then(pourOut)
// .catch((error) => {
// console.error("Error:", error);
// });
// Using Promise.all
// const getCoffee = () => {
// return Promise.all([boilWater(), grindingCoffeeBean(),addSugar(), mixAllIngredients()])
// .then(pourOut)
// .catch(()=>{
// console.log("coffee isn't ready ")
// })
// }
// getCoffee()
// async await
const getCoffee = async () => {
try {
await grindingCoffeeBean();
await boilWater();
await addSugar();
await mixAllIngredients();
pourOut();
} catch (error) {
console.error("Error:", error);
}
};
getCoffee();