forked from Kashyap-Rishi/Gdsc_practice-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
152 lines (85 loc) · 3.07 KB
/
sync.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//creating objects
let stocks = {
//creating arrays inside objects
Fruits: ["strawberry", "grapes", "banana", "apple"],
toppings: ["chocolate", "peanuts"]
};
let is_shop_open = true;
let order = (time, work) => {
return new Promise((resolve, reject) => {
if (is_shop_open) {
setTimeout(() => {
resolve(work()); //resolve is used to signal the successful completion of an asynchronous task, and work() represents the actual task to be performed.
}, time);
} else {
reject(console.log("our shop is closed"));
}
});
};
// order(2000, () => console.log(`${stocks.Fruits[0]} was selected`))
// .then(()=>{
// return order(2000, ()=>console.log("The fruit was chopped"));
// })
// .then(()=>{
// return order(1000, ()=>console.log("Start the machine"));
// })
// .then(()=>{
// return order(3000,()=>{
// console.log(` ${stocks.toppings[0]} was selected`)
// })
// })
// .then(()=>{
// return order(1000, ()=>console.log("ice ream was served"));
// })
// .catch(()=>{
// console.log("Customer left");
// })
// .finally(()=>{
// console.log("day ended, shop is closed")
// })
//Inside the order function, when the time delay is completed (i.e., after 2000 milliseconds), it calls resolve(work()). Here, work() is the callback function . So, in this context, resolve is saying that the work represented by () => console.log(${stocks.Fruits[0]} was selected) has been successfully completed, and any subsequent .then callbacks will be executed.
// Async and await
// async function kitchen(){
// console.log("a");
// console.log("b");
// console.log("c");
// order(2000, () => console.log(`${stocks.Fruits[0]} was selected`))
// console.log("d");
// console.log("e");
// }
// kitchen();
// console.log("Doing the dishes");
// console.log("Cleaning the tables");
// console.log("Taking other's order");
//Updated example --
// function time(ms){
// return new Promise((resolve, reject)=>{
// if(is_shop_open){
// setTimeout(resolve,ms) //it creates a promise that resolves after a specified delay.This means that after the delay, the promise will be resolved without providing any specific value or result.
// }
// else{
// reject(console.log("shop is closed"));
// }
// });
// }
// async function kitchen(){
// try{
// await time(2000);
// console.log(`${stocks.Fruits[0]} was selected`)
// await time(2000)
// console.log("Cut the fruit");
// await time(1000)
// console.log("Start the machine")
// await time(3000)
// console.log(` ${stocks.toppings[0]} was selected`)
// await time(2000)
// console.log("Icecream was served")
// }
// catch(error){
// console.log("Customer left", error)
// }
// finally{
// console.log("day ended, shop is closed")
// }
// }
// kitchen();