Skip to content

Commit 20a7eab

Browse files
callback hell is resolved by promises
1 parent da16b14 commit 20a7eab

File tree

1 file changed

+92
-31
lines changed
  • async-await_callbacks_promises

1 file changed

+92
-31
lines changed

async-await_callbacks_promises/app.js

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,106 @@
4040

4141
// one(two)
4242

43+
// let stocks = {
44+
// Fruits: ["strawberry", "grapes", "banana", "apple"],
45+
// liquid: ["water", "ice"],
46+
// holder: ["cone", "cup", "stick"],
47+
// toppings: ["chocolate", "peanuts"],
48+
// };
49+
50+
// let order = (Fruit_name, callproduction) => {
51+
52+
// setTimeout(() => {
53+
// console.log(`${stocks.Fruits[Fruit_name]} was selected`)
54+
// callproduction();
55+
// }, 2000)
56+
57+
// };
58+
59+
// let production = () => {
60+
61+
// setTimeout(() => {
62+
// console.log("Production has started")
63+
// setTimeout(() => {
64+
// console.log("the fruit has been chopped.")
65+
// setTimeout(() => {
66+
// console.log(`${stocks.liquid[1]} and ${stocks.liquid[0]} was added`);
67+
// setTimeout(() => {
68+
// console.log(`the machine was started`);
69+
// setTimeout(() => {
70+
// console.log(`${stocks.holder[0]} was selected`);
71+
// setTimeout(()=>{
72+
// console.log(`${stocks.toppings[0]}was added as toppings`);
73+
// setTimeout(()=>{
74+
// console.log(`serve ice cream`)
75+
// },2000);
76+
// },3000);
77+
// }, 2000);
78+
// }, 1000);
79+
// }, 1000);
80+
// }, 2000);
81+
// }, 0);
82+
// };
83+
84+
// order(0, production);
85+
4386
let stocks = {
4487
Fruits: ["strawberry", "grapes", "banana", "apple"],
4588
liquid: ["water", "ice"],
4689
holder: ["cone", "cup", "stick"],
4790
toppings: ["chocolate", "peanuts"],
4891
};
4992

50-
let order = (Fruit_name, callproduction) => {
93+
let is_shop_open = true;
5194

52-
setTimeout(() => {
53-
console.log(`${stocks.Fruits[Fruit_name]} was selected`)
54-
callproduction();
55-
}, 2000)
95+
let order = (time,work)=>{
5696

57-
};
97+
return new Promise( (resolve,reject)=>{
98+
if(is_shop_open){
99+
setTimeout(()=>{
100+
resolve(work())
101+
},time)
102+
}
103+
else{
104+
reject(console.log("our shop is closed"))
105+
}
106+
} )
107+
}
58108

59-
let production = () => {
60-
61-
setTimeout(() => {
62-
console.log("Production has started")
63-
setTimeout(() => {
64-
console.log("the fruit has been chopped.")
65-
setTimeout(() => {
66-
console.log(`${stocks.liquid[1]} and ${stocks.liquid[0]} was added`);
67-
setTimeout(() => {
68-
console.log(`the machine was started`);
69-
setTimeout(() => {
70-
console.log(`${stocks.holder[0]} was selected`);
71-
setTimeout(()=>{
72-
console.log(`${stocks.toppings[0]}was added as toppings`);
73-
setTimeout(()=>{
74-
console.log(`serve ice cream`)
75-
},2000);
76-
},3000);
77-
}, 2000);
78-
}, 1000);
79-
}, 1000);
80-
}, 2000);
81-
}, 0);
82-
};
109+
order(2000,()=>console.log(`${stocks.Fruits[0]} was selected`))
110+
111+
.then(()=>{
112+
return order(0,()=>console.log(`production has started`))
113+
})
114+
115+
.then(()=>{
116+
return order(2000,()=>console.log(`The fruit was chopped`))
117+
})
118+
119+
.then(()=>{
120+
return order(1000,()=>console.log(`added water and ice`))
121+
})
122+
123+
.then(()=>{
124+
return order(1000,()=>console.log(`started the machine`))
125+
})
126+
127+
.then(()=>{
128+
return order(2000,()=>console.log(`selected the cup`))
129+
})
130+
131+
.then(()=>{
132+
return order(3000,()=>console.log(`${stocks.toppings[0]} was selected as a topping`))
133+
})
134+
135+
.then(()=>{
136+
return order(2000,()=>console.log(`ice cream was served`))
137+
})
138+
139+
.catch(()=>{
140+
console.log(`Customer was left`)
141+
})
83142

84-
order(0, production);
143+
.finally(()=>{
144+
console.log(`day ended, shop is closed`)
145+
})

0 commit comments

Comments
 (0)