-
Notifications
You must be signed in to change notification settings - Fork 1
/
callbacks.js
49 lines (40 loc) · 890 Bytes
/
callbacks.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
// function doStep1(init) {
// return init + 1;
// }
// function doStep2(init) {
// return init + 2;
// }
// function doStep3(init) {
// return init + 3;
// }
// function doOperation() {
// // synchronous code
// let result = 0;
// result = doStep1(result); // 1
// result = doStep2(result); // 3
// result = doStep3(result); // 6
// console.log(`result: ${result}`); // 6
// }
// doOperation();
function doStep1(init, callback) {
const result = init + 1; //1
callback(result);
}
function doStep2(init, callback) {
const result = init + 2;
callback(result);
}
function doStep3(init, callback) {
const result = init + 3;
callback(result);
}
function doOperation() {
doStep1(0, (result1) => {
doStep2(result1, (result2) => {
doStep3(result2, (result3) => {
console.log(`result: ${result3}`);
});
});
});
}
doOperation();