forked from thalesmello/exploring-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_callback_hell.js
37 lines (33 loc) · 922 Bytes
/
02_callback_hell.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
var aBootTime = 1000,
bBootTime = 1000,
queueCallback = null,
serverHandler = null;
console.log("A: Booting up system...")
setTimeout(() => {
console.log("A: Checking network connection");
setTimeout(() => {
console.log("A: Request complex computation");
sendRequest(value => {
console.log("A: Computation returned " + value);
});
}, 500);
}, aBootTime);
console.log("B: Booting up system...")
setTimeout(() => {
console.log("B: Server up and running");
serverHandler = (callback) => {
console.log("B: Starting heavy computation");
setTimeout(() => callback(42), 2000)
}
if (queueCallback) {
serverHandler(queueCallback);
queueCallback = null;
}
}, bBootTime);
function sendRequest(callback) {
if(serverHandler) {
serverHandler(callback);
} else {
queueCallback = callback;
}
}