-
Notifications
You must be signed in to change notification settings - Fork 0
/
day26.js
116 lines (85 loc) · 1.68 KB
/
day26.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
// callback hell recape
let mynum=[23,45,67,88,89,-3,-5,-78]
let posnum= negavativenum(mynum,(x)=>x>0)
console.log(posnum);
function negavativenum(mynum,callback){
let myarr=[]
for(let item of mynum){
if(callback(item)){
myarr.push(item)
}
}
return myarr
}
function sum(x,y,callbacks){
let rese=x+y;
callbacks(rese)
}
let result=sum(4,6,(rese)=>{
console.log("result :",rese);
})
// promises recape
let stocks = {
Fruits : ["strawberry", "grapes", "banana", "apple"],
liquid : ["water", "ice"],
holder : ["cone", "cup", "stick"],
toppings : ["chocolate", "peanuts"],
};
let is_shop_open = true;
let order =(time,work)=>{
return new Promise((resolve,reject)=>{
if(is_shop_open){
setTimeout(()=>{
resolve(work())
},time)
// resolve(200)
}
else{
// reject(404)
reject(console.log("shop is closed"))
}
})
}
order(2000,()=>console.log(`the function ${stocks.Fruits[0]}`))
.then(()=>{
return order(1000 ,()=>{
console.log("production has started");
})
})
.then(()=>{
return order(2000,()=>{
console.log("the fruit was chopped");
})
})
.then(()=>{
return order(2000,()=>{
console.log("add water and ice");
})
})
.then(()=>{
return order(2000,()=>{
console.log("start the machine");
})
})
.then(()=>{
return order(1000,()=>{
console.log("select container");
})
})
.then(()=>{
return order(3000,()=>{
console.log("toppings added");
})
})
.then(()=>{
return order(2000,()=>{
console.log("serve ice cream");
})
})
.catch(()=>{
console.log("customer left");
// console.log(err);
})
.finally(()=>{
console.log("finally process ends");
})