|
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 |
9 | 2 |
|
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"); |
12 | 7 |
|
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", () => { |
15 | 20 | setTimeout(() => { |
16 | | - console.log("boiling water done."); |
17 | | - console.log("add carrots, boiling..."); |
| 21 | + heading1.style.color = "red"; |
18 | 22 | setTimeout(() => { |
19 | | - console.log("carrots are done"); |
20 | | - console.log("add onions, boiling..."); |
| 23 | + heading2.style.color = "green"; |
21 | 24 | 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 | +}); |
0 commit comments