-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbackHell.js
46 lines (42 loc) · 1.04 KB
/
callbackHell.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
/**
* Callback Hell - Situation in JavaScript where callbacks
* are nested within other callbacks to the
* degree where the code is difficult to read .
* Old pattern to handle asynchronous functions.
* Use Promisses + async/await to avoid Callback Hell
*/
function task1(callback) {
setTimeout(() => {
console.log("taks 1 complete")
callback();
}, 2000)
}
function task2(callback) {
setTimeout(() => {
console.log("taks 2 complete")
callback();
}, 400)
}
function task3(callback) {
setTimeout(() => {
console.log("taks 3 complete")
callback();
}, 1000)
}
function task4(callback) {
setTimeout(() => {
console.log("taks 4 complete")
callback();
}, 100)
}
// Nesting callbacks inside another callbacks
// 4 levels of callback is near the limit
task1(() => {
task2(() => {
task3(() => {
task4(() => {
console.log("All tasks completed")
})
})
})
})