-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEp-15-Async-Event-Loop.js
More file actions
55 lines (40 loc) · 1.63 KB
/
Ep-15-Async-Event-Loop.js
File metadata and controls
55 lines (40 loc) · 1.63 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
function a () {
console.log("a");
}
a()
console.log("End");
// Callstack is in js Engine is in Browser
console.log(("Start"));
setTimeout(function cb () {
console.log("Callback");
},5000)
console.log("End");
console.log("Start");
document.getElementById("clickMe").addEventListener("click",function cb () {
console.log("Callback")
})
console.log("End");
// 🟨Event Loop : Continuously monitor callstack and callback queue
// ... If this call stack is empty and EventLoop sees this there is also a function waiting to be executed inside callbackQueue...
// it just takes the function and put it inside the callstack... and callback method is quickly executed...
// Promises Mutation Observer
console.log("start");
setTimeout(function cbT () {
console.log("CB Netflix");
},5000);
fetch("https://api.netflix.com").then(function cbF () {
console.log("CB Netflix");
})
console.log("End");
// Callstack : │ │
// │ │
// │ │
// │ │
// │ 2nd cbT => Run => Removed from callstack │
// │ 1st cbF => Run => Removed from callstack │
// Micro Task Que :│ cbF │ │ │ │ │ │ │ │ │ │ │ │ │ // prioritized 1st goes into callstack
// Callback Queue :│ cbT │ │ │ │ │ │ │ │ │ │ │ │ │ // after cbF
// 1 Start
// 2 End
// 3 cbF
// 4 cbT