-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (184 loc) · 5.18 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//storing our stocks
let stocks = {
fruits: ['apple', 'banana', 'mango', 'graps', 'strawberry', 'cucumber'],
liquid:['water', 'ice'],
holder: ['cone', 'cup', 'sticks', 'glass'],
toppings: ['chocolate', 'peanuts', 'sprinkals'],
}
/****************************************************************
// order method
let order = (fruitName, callProduction) => {
console.log("order placed, Please call for production");
// select fruits
setTimeout(() => {
console.log(`${stocks.fruits[fruitName]} has selected.` )
}, 2000);
//call for production
callProduction();
}
// this is callback hell...to save us from callbacks,
//Promises come to picture...
// production method
let production = () => {
console.log("order received");
setTimeout(() => {
console.log("production started...")
// fruits chopped
setTimeout(() => {
console.log("the fruit has been chopped.");
//adding ice and water
setTimeout(() => {
console.log(`${stocks.liquid[0]} and ${stocks.liquid[1]} was added.`)
// use machine
setTimeout(() => {
console.log("machine is started...")
//choose container
setTimeout(() => {
console.log(`${stocks.holder[0]} has selected.`)
// ice-cream filled into container
setTimeout(() => {
console.log(`${stocks.toppings[0]}`)
//serve the ice cream
setTimeout(() => {
console.log("ice-cream has served...");
}, 2000);
}, 2000);
}, 1000);
}, 1000);
}, 2000)
}, 2000)
}, 0000);
}
// call order() to start production
order(0, production);
*****************************************************/
/************************************************
// solving using promise
let isShopOpen = true;
// order() invoke promise
let order = (time, work) => {
// creating promise
return new Promise( (resolve, reject) => {
if(isShopOpen){
setTimeout(() => {
resolve(work());
}, time);
}else{
reject(console.log("our shope is closed!"));
}
})
}
// invoke order() and get promise result so apply then(),catch() and finaly()
order(2000, () => console.log(`${stocks.fruits[1]}`))
.then(() => {
return order(0000, () => console.log("production has started"));
})
.then(() => {
return order(2000, () => console.log("fruits was chopped"));
})
.then(() => {
return order(3000, () => console.log(`${stocks.liquid[0]}`));
})
.then(()=> {
return order(2000, () => console.log("machine started"));
})
.then(() => {
return order(1000, () => console.log(`${stocks.holder[1]}`));
})
.then(()=> {
return order(2000, () => console.log("ice cream filled"));
})
.then(() => {
return order(3000, () => console.log("ice-cream has served."));
})
.catch(() => {
console.log("customer returned")
})
.finally(() => {
console.log("day ended, shop is closed");
});
************************************************************/
/*************************************************** */
// // creating promise like this
// let isShopOpen = ture;
// let order = () => {
// return new Promise((resolve, reject) => {
// if(isShopOpen){
// resolve();
// }else{
// reject();
// }
// })
// }
// order()
// .then()
// .then()
// .catch()
// .finally()
// // using async/ await to solve
// let isShopOpen = true;
// async function order () {
// try{
// await abc;
// }
// catch(error){
// console.log("abc doesn't exit", error);
// }
// finally{
// console.log("run every time no matter what...");
// }
// }
// order()
// .then(()=> {console.log("Hello")})
// .then(()=> {console.log("javascript")})
// .catch((err) => {console.log(err)})
// .finally(()=> {console.log("finally")});
// let isShopOpen = true;
// let toppingChoice = () => {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(console.log("what do you like ?"));
// }, 2000);
// });
// };
// async function kitchen() {
// console.log("work1 done");
// console.log("work2 done");
// console.log("work3 done");
// // next work is hold bcz of await
// await toppingChoice();
// console.log("work4 done after await executed");
// console.log("work5 done after await");
// }
// kitchen();
// console.log("work is not held bcz of await");
// console.log("work is happening");
let isShopOpen = false;
function time(ms) {
return new Promise ((resolve, reject) =>{
if(isShopOpen){
setTimeout(resolve, ms);
}else{
reject(console.log("shop has closed"));
}
});
}
async function kitchen(){
try{
await time(2000);
console.log(`${stocks.fruits[1]}`);
await time(0000);
console.log('start production');
await time(3000);
console.log('fruits chopped');
await time(1000);
console.log('filled in container');
}
catch(err){
console.log(err);
}
finally{
console.log('finally always run');
}
}
kitchen();