-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback.js
138 lines (121 loc) · 3.83 KB
/
callback.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
pizzaProbability = 97; // declared in main.js
/* Our asynchronous function that takes a callback */
function makeClassicPizza (customer, callback) {
/* setTimeout is used to simulate unknown delay */
setTimeout( function() {
const pizzaHasBeenMade = pizzaMakingEvent();
console.log(`${customer}'s pizza has been made:`, pizzaHasBeenMade);
if (pizzaHasBeenMade) {
callback ({
status: 'success',
msg: customer + ' Success! ',
});
}
else {
callback ({
status: 'failure',
msg: 'there was a problem with ' + customer + '\'s pizza.',
});
}
}, lag(minPizzaLag, maxPizzaLag));
}
// function serveTableSingleCustomer() {
// orderPlaced('single');
// makeClassicPizza('Alice', result => {
// if (result.status === 'success') {
// let alicePizzaMade = result;
// console.log(alicePizzaMade.msg)
// orderServed('single')
// } else {
// oops(result.msg)
// }
// })
// }
// serveTableSingleCustomer()
// function serveTableFromSingleSmallOven() {
// orderPlaced('sequential-callbacks');
// // Sequential
// // there's only one oven, so each pizza needs to be made and cooked
// // before the next pizza
// // and we only want to serve once they're all cooked
// let alicePizzaMade = undefined;
// let bobbyPizzaMade = undefined;
// let charliePizzaMade = undefined;
// makeClassicPizza('Alice', aliceResult => {
// if (aliceResult.status === 'success') {
// alicePizzaMade = aliceResult;
// makeClassicPizza('Bobby', bobbyResult => {
// if (bobbyResult.status === 'success') {
// bobbyPizzaMade = bobbyResult;
// makeClassicPizza('Charlie', charlieResult => {
// if (charlieResult.status === 'success') {
// charliePizzaMade = charlieResult;
// orderServed('sequential-callbacks')
// // console.log(alicePizzaMade.msg)
// // console.log(bobbyPizzaMade.msg)
// // console.log(charliePizzaMade.msg)
// } else {
// oops(charlieResult.msg)
// }
// })
// } else {
// oops(bobbyResult.msg)
// }
// })
// } else {
// oops(aliceResult.msg)
// }
// })
// }
// serveTableFromSingleSmallOven()
// function serveTableFromMultipleLargeOvens() {
// orderPlaced('consecutive-callbacks')
// // Mostly consecutive
// // there are lots of ovens, so each pizza can be made and cooked
// // at the same time as the other's pizzas
// // but we still only want to serve once they're all cooked
// let alicePizzaMade = undefined;
// let bobbyPizzaMade = undefined;
// let charliePizzaMade = undefined;
// function isOrderDone() {
// if (alicePizzaMade && bobbyPizzaMade && charliePizzaMade) {
// if (alicePizzaMade.status === 'success' &&
// bobbyPizzaMade.status === 'success' &&
// charliePizzaMade.status === 'success') {
// orderServed('consecutive-callbacks')
// // console.log(alicePizzaMade)
// // console.log(bobbyPizzaMade)
// // console.log(charliePizzaMade)
// } else if (alicePizzaMade.status === 'failure' ||
// bobbyPizzaMade.status === 'failure' ||
// charliePizzaMade.status === 'failure') {
// console.log('Sorry, the table\'s gonna be hungry for a while longer while we start from scratch.')
// }
// }
// }
// makeClassicPizza('Alice', aliceResult => {
// if (aliceResult.status === 'success') {
// alicePizzaMade = aliceResult;
// isOrderDone();
// } else {
// oops(aliceResult.msg)
// }
// })
// makeClassicPizza('Bobby', bobbyResult => {
// if (bobbyResult.status === 'success') {
// bobbyPizzaMade = bobbyResult;
// isOrderDone();
// } else {
// oops(bobbyResult.msg)
// }
// })
// makeClassicPizza('Charlie', charlieResult => {
// if (charlieResult.status === 'success') {
// charliePizzaMade = charlieResult;
// isOrderDone();
// } else {
// oops(charlieResult.msg)
// }
// })
// }
// serveTableFromMultipleLargeOvens()