Skip to content

Commit dd2863c

Browse files
committed
Callback Hell - DOM Example
1 parent db6b438 commit dd2863c

3 files changed

Lines changed: 33 additions & 25 deletions

File tree

app.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
/* Make soup */
2-
// boil water for 10 min
3-
// chop carrots
4-
// add carrots boil for 5 min
5-
// chop onions
6-
// add onion boil for 5 min
7-
// BROWSER!!!!! Fetch Data, Get Geolocation, setTimeout, setTimer etc
8-
// These are coming from browser not JavaScript. So, JavaScript gives this functionality to browser to handle, once it's doneit comes to JavaScript to execute if it is not busy
1+
// callbacks, promises, async/await
92

10-
boilWater();
11-
console.log("chop carrots");
3+
const heading1 = document.querySelector(".heading-1");
4+
const heading2 = document.querySelector(".heading-2");
5+
const heading3 = document.querySelector(".heading-3");
6+
const btn = document.querySelector(".btn");
127

13-
function boilWater() {
14-
console.log("boiling...");
8+
/* Initial Example */
9+
// btn.addEventListener("click", () => {
10+
// console.log("You clicked the button.");
11+
// });
12+
13+
// console.log("I'm second");
14+
// for (let i = 0; i < 10000; i++) {
15+
// console.log("I am busy");
16+
// }
17+
18+
/* Second Example - The challenge here second one should start executing once first one completes and same goes for third to second*/
19+
btn.addEventListener("click", () => {
1520
setTimeout(() => {
16-
console.log("boiling water done.");
17-
console.log("add carrots, boiling...");
21+
heading1.style.color = "red";
1822
setTimeout(() => {
19-
console.log("carrots are done");
20-
console.log("add onions, boiling...");
23+
heading2.style.color = "green";
2124
setTimeout(() => {
22-
console.log("onions are done");
23-
}, 5000);
24-
}, 5000);
25-
console.log("chop onions");
26-
}, 10000);
27-
}
28-
29-
// The more things we need to run sequentially, the more nesting callbacks it becomes, which is hard to understand i.e. callback hell
25+
heading3.style.color = "blue";
26+
}, 1000);
27+
}, 2000);
28+
}, 1000);
29+
});

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
<link rel="stylesheet" href="./styles.css" />
1212
</head>
1313
<body>
14-
<h1>Asynchronous JavaScript</h1>
14+
<h1>asynchronous javaScript</h1>
15+
<h1 class="heading-1">hello world</h1>
16+
<h1 class="heading-2">hello people</h1>
17+
<h1 class="heading-3">hello javaScript</h1>
18+
<button type="button" class="btn">click me</button>
1519

1620
<!-- Link to JavaScript -->
1721
<script type="module" src="./app.js"></script>

styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ h1 {
99
text-align: center;
1010
letter-spacing: 2px;
1111
}
12+
13+
.btn {
14+
text-transform: capitalize;
15+
}

0 commit comments

Comments
 (0)