-
Notifications
You must be signed in to change notification settings - Fork 0
/
Callback_Function_and_Callback_Hell.js
123 lines (104 loc) · 5.14 KB
/
Callback_Function_and_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
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
function mainFunction(callback) {
console.log("Performing operation...");
// Use setTimeout to simulate an asynchronous operation
setTimeout(function() {
callback("Operation complete");
}, 1000);
}
// Define the callback function
function callbackFunction(result) {
console.log("Result: " + result);
}
// Call the main function with the callback function
mainFunction(callbackFunction);
function divideByHalf(sum){
console.log(Math.floor(sum / 2));
}
function multiplyBy2(sum){
console.log(sum * 2);
}
function operationOnSum(num1,num2,operation){
var sum = num1 + num2;
operation(sum);
}
operationOnSum(3, 3, divideByHalf); // Outputs 3
operationOnSum(5, 5, multiplyBy2); // Outputs 20
// CALLBACK HELL
// Callbacks
function loadScript(src, callback) {
var script = document.createElement("script");
script.src = src;
script.onload = function() {
console.log("Loaded script with SRC: " + src)
callback(null, src);
}
script.onerror = function() {
console.log("Error loading script with SRC: " + src);
callback(new Error("Src got some error"))
}
document.body.appendChild(script);
}
function hello(error, src) {
if (error) {
console.log(error)
return
}
alert('Hello World!' + src);
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap2.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap3.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap4.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap5.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap6.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap7.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap8.bundle.min.js", function goodmorning(error, src) {
if (error) {
console.log(error)
sendEmergencyMessageToCeo();
return
}
loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap9.bundle.min.js", function goodmorning(error, src) { })
})
})
})
})
})
})
})
})