8383
8484// order(0, production);
8585
86- let stocks = {
87- Fruits : [ "strawberry" , "grapes" , "banana" , "apple" ] ,
88- liquid : [ "water" , "ice" ] ,
89- holder : [ "cone" , "cup" , "stick" ] ,
90- toppings : [ "chocolate" , "peanuts" ] ,
91- } ;
86+ // let stocks = {
87+ // Fruits: ["strawberry", "grapes", "banana", "apple"],
88+ // liquid: ["water", "ice"],
89+ // holder: ["cone", "cup", "stick"],
90+ // toppings: ["chocolate", "peanuts"],
91+ // };
9292
93- let is_shop_open = true ;
93+ // let is_shop_open = true;
9494
95- let order = ( time , work ) => {
96-
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- }
95+ // let order = (time,work)=>{
96+
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+ // }
108+
109+ // order(2000,()=>console.log(`${stocks.Fruits[0]} was selected`))
108110
109- order ( 2000 , ( ) => console . log ( `${ stocks . Fruits [ 0 ] } was selected` ) )
111+ // .then(()=>{
112+ // return order(0,()=>console.log(`production has started`))
113+ // })
110114
111- . then ( ( ) => {
112- return order ( 0 , ( ) => console . log ( `production has started ` ) )
113- } )
115+ // .then(()=>{
116+ // return order(2000 ,()=>console.log(`The fruit was chopped `))
117+ // })
114118
115- . then ( ( ) => {
116- return order ( 2000 , ( ) => console . log ( `The fruit was chopped ` ) )
117- } )
119+ // .then(()=>{
120+ // return order(1000 ,()=>console.log(`added water and ice `))
121+ // })
118122
119- . then ( ( ) => {
120- return order ( 1000 , ( ) => console . log ( `added water and ice ` ) )
121- } )
123+ // .then(()=>{
124+ // return order(1000,()=>console.log(`started the machine `))
125+ // })
122126
123- . then ( ( ) => {
124- return order ( 1000 , ( ) => console . log ( `started the machine ` ) )
125- } )
127+ // .then(()=>{
128+ // return order(2000 ,()=>console.log(`selected the cup `))
129+ // })
126130
127- . then ( ( ) => {
128- return order ( 2000 , ( ) => console . log ( `selected the cup ` ) )
129- } )
131+ // .then(()=>{
132+ // return order(3000 ,()=>console.log(`${stocks.toppings[0]} was selected as a topping `))
133+ // })
130134
131- . then ( ( ) => {
132- return order ( 3000 , ( ) => console . log ( `${ stocks . toppings [ 0 ] } was selected as a topping ` ) )
133- } )
135+ // .then(()=>{
136+ // return order(2000 ,()=>console.log(`ice cream was served `))
137+ // })
134138
135- . then ( ( ) => {
136- return order ( 2000 , ( ) => console . log ( `ice cream was served` ) )
137- } )
139+ // .catch (()=>{
140+ // console.log(`Customer was left` )
141+ // })
138142
139- . catch ( ( ) => {
140- console . log ( `Customer was left` )
141- } )
143+ // .finally(()=>{
144+ // console.log(`day ended, shop is closed`)
145+ // })
146+
147+
148+ let stocks = {
149+ Fruits : [ "strawberry" , "grapes" , "banana" , "apple" ] ,
150+ liquid : [ "water" , "ice" ] ,
151+ holder : [ "cone" , "cup" , "stick" ] ,
152+ toppings : [ "chocolate" , "peanuts" ] ,
153+ } ;
154+
155+ let is_shop_open = true ;
156+
157+ // let order = () =>{
158+ // return new Promise((res,rej)=>{
159+ // if(true){
160+ // res()
161+ // }
162+ // else{
163+ // rej()
164+ // }
165+ // })
166+ // }
167+
168+ function order ( time , work ) {
169+ return new Promise ( ( resolve , reject ) => {
170+ if ( is_shop_open ) {
171+ setTimeout ( ( ) => {
172+ resolve ( work ( ) ) ;
173+ } , time ) ;
174+ } else {
175+ reject ( "Shop is closed" ) ;
176+ }
177+ } ) ;
178+ }
179+
180+ async function kitchen ( ) {
181+ try {
182+ await order ( 2000 , ( ) => console . log ( `${ stocks . Fruits [ 0 ] } was selected` ) ) ;
183+ await order ( 0 , ( ) => console . log ( "Production has started" ) ) ;
184+ await order ( 2000 , ( ) => console . log ( "Fruit chopped" ) ) ;
185+ await order ( 1000 , ( ) => console . log ( "Added water & ice" ) ) ;
186+ await order ( 1000 , ( ) => console . log ( "Machine started" ) ) ;
187+ await order ( 2000 , ( ) => console . log ( "Selected the cup" ) ) ;
188+ await order ( 3000 , ( ) => console . log ( `${ stocks . toppings [ 0 ] } added` ) ) ;
189+ await order ( 2000 , ( ) => console . log ( "Ice cream served" ) ) ;
190+ } catch ( err ) {
191+ console . log ( "Customer left:" , err ) ;
192+ } finally {
193+ console . log ( "Day ended, shop is closed" ) ;
194+ }
195+ }
142196
143- . finally ( ( ) => {
144- console . log ( `day ended, shop is closed` )
145- } )
197+ kitchen ( ) ;
0 commit comments