-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.html
79 lines (62 loc) · 1.98 KB
/
promises.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let stocks={
Fruits: ["strawberry","grapes","banana","apple"],
liquid : ["water","ice"],
holder : ["cone","cup","stick"],
toppings : ["chocolate","penuts"]
};
let is_shop_open = true;
let order = (time,work) =>{
return new Promise( (resolve,reject)=>{
if(is_shop_open){
setTimeout( ()=>{
resolve( work() )
},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(`${stocks.liquid[0]} and ${stocks.liquid[1]} was selected`)
})
})
.then(()=>{
return order(1000, () =>console.log("start the machine"))
})
.then(()=>{
return order(2000, () =>{
console.log(`ice cream placed on${stocks.holder[0]}`)
})
})
.then(()=>{
return order(3000, ()=>{
console.log(`${stocks.toppings[0]}was selected`)
})
})
.then (()=>{
return order(1000, ()=>console.log("ice cream was served"))
})
.catch(()=>{
console.log("customer left")
})
.finally(()=>{
console.log("day ended,shop is closed")
});
</script>
</body>
</html>