Skip to content

Commit 0fb475f

Browse files
committed
EP-15 | Asynchronous Js & Event Loop
1 parent 1f08cf2 commit 0fb475f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function a () {
2+
console.log("a");
3+
}
4+
a()
5+
console.log("End");
6+
7+
// Callstack is in js Engine is in Browser
8+
9+
console.log(("Start"));
10+
11+
setTimeout(function cb () {
12+
console.log("Callback");
13+
},5000)
14+
console.log("End");
15+
16+
console.log("Start");
17+
document.getElementById("clickMe").addEventListener("click",function cb () {
18+
console.log("Callback")
19+
})
20+
console.log("End");
21+
22+
// 🟨Event Loop : Continuously monitor callstack and callback queue
23+
// ... If this call stack is empty and EventLoop sees this there is also a function waiting to be executed inside callbackQueue...
24+
// it just takes the function and put it inside the callstack... and callback method is quickly executed...
25+
26+
// Promises Mutation Observer
27+
28+
console.log("start");
29+
setTimeout(function cbT () {
30+
console.log("CB Netflix");
31+
},5000);
32+
33+
fetch("https://api.netflix.com").then(function cbF () {
34+
console.log("CB Netflix");
35+
})
36+
console.log("End");
37+
38+
39+
// Callstack : │ │
40+
// │ │
41+
// │ │
42+
// │ │
43+
// │ 2nd cbT => Run => Removed from callstack │
44+
// │ 1st cbF => Run => Removed from callstack │
45+
46+
47+
// Micro Task Que :│ cbF │ │ │ │ │ │ │ │ │ │ │ │ │ // prioritized 1st goes into callstack
48+
49+
// Callback Queue :│ cbT │ │ │ │ │ │ │ │ │ │ │ │ │ // after cbF
50+
51+
52+
// 1 Start
53+
// 2 End
54+
// 3 cbF
55+
// 4 cbT

0 commit comments

Comments
 (0)