-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallBack_Hell.js
More file actions
61 lines (44 loc) · 1.15 KB
/
CallBack_Hell.js
File metadata and controls
61 lines (44 loc) · 1.15 KB
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
// Callback Hell = situation in java script where callbacks
// are nested inside other callbacks to the
// degree where the code is difficult to read
// old pattern to handle asynchronous fucntions .
// use promises + asynchronous + async / await to avoid callback hell.
function task1(callback){
setTimeout(() => {
console.log('ts1 is completed.');
callback();
}, 2000);
}
function task2(callback){
setTimeout(()=>{
console.log(`Task2 is completed.`);
callback()
},1000)
}
function task3(callback){
setTimeout(() => {
console.log('task3 is completed.') ;
callback()
}, 3000);
}
function task4(callback){
setTimeout(()=>{
console.log(`task4 is completed .`);
callback()
},4000)
}
function task5(callback){
setTimeout(()=>{
console.log(`task5 is completed .`);
callback()
},6000)
}
task1(()=>{
task2(() =>{
task3(() => {
task4(()=>{
task5(()=> console.log(`All task completed`));
});
});
});
});