Skip to content

Commit 556ad5d

Browse files
Update 16-Async-EventLoops.md
1 parent 90fda4b commit 556ad5d

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Notes/16-Async-EventLoops.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,95 @@ We get all these inside call stack through *global object* ie. **window**
1818
- Use window keyword like : window.setTimeout(), window.localstorage, window.console.log() to log something inside console.
1919
- As window is global obj, and all the above functions are present in global object, we don't explicity write *window* but it is implied
2020

21+
### Workflow
22+
23+
```
24+
// First a GEC is created and put inside call stack.
25+
console.log("Start"); // this calls the console web api (through window) which in turn actually modifies values in console.
26+
27+
setTimeout(function cb() { //this calls the setT web api which gives access to timer feature. It stores the callback cb() and starts timer.
28+
console.log("Callback");
29+
}, 5000);
30+
31+
console.log("End"); // calls console api and logs in console window. After this GEC pops from call stack.
32+
33+
```
34+
- While all this is happening, the timer is constantly ticking. After it becomes 0, the callback cb() has to run.
35+
- Now we need this cb to go into call stack. Only then will it be executed. For this we need **event loop and Callback queue**
36+
37+
### Event Loops and Callback Queue
38+
- cb() cannot simply directly go to callstack to be execeuted. It goes through the callback queue when timer expires.
39+
- Event loop checks the callback queue, and if it has element puts it into call stack. It is a *gate keeper*
40+
- Now cb() in callstack is run. Console API is used and log printed
41+
42+
Final console output:
43+
44+
> Start
45+
46+
> End
47+
48+
> Callback
49+
50+
### Same happens for any other event as well (Click, Hover etc). This is the basic workflow.
51+
52+
```
53+
console.log("Start");
54+
document. getElementById("btn").addEventListener("click", function cb() { // cb() registered inside webapi environment and event(click) attached to it. ie.
55+
// REGISTERING CALLBACK AND ATTACHING EVENT TO IT.
56+
console.log("Callback");
57+
});
58+
59+
console.log("End"); // calls console api and logs in console window. After this GEC pops from call stack.
60+
61+
```
62+
In above code, even after console prints "Start" and "End" and pops GEC out, **the eventListener stays in webapi env**(with hope that user may click it
63+
some day) until explicitly removed, or the browser is closed.
64+
65+
### Why need callback queue?
66+
- Why can't event loop directly take cb() and put it in callstack? Suppose user clciks button x6 times. So 6 cb() are put inside callback queue.
67+
- Event loop sees if call stack is empty/has space and whether callback queue is not empty(6 elements here).
68+
- Elements of callback queue popped off, put in callstack, executed and then popped off from call stack.
69+
70+
### fetch() works a bit different than the rest
71+
```
72+
console.log("Start"); // this calls the console web api (through window) which in turn actually modifies values in console.
73+
74+
setTimeout(function cbT() {
75+
console.log("CB Timeout");
76+
}, 5000);
77+
78+
fetch("https://api.netflix.com").then(function cbF() {
79+
console.log("CB Netflix");
80+
});
81+
82+
console.log("End");
83+
84+
```
85+
- Same steps for everything before fetch() in above code.
86+
- fetch registers cbF into webapi environment along with existing cbT.
87+
- cbT is waiting for 5000ms to end so that it can be put inside callback queue. cbF is waiting for data to be returned from Netflix servers.
88+
- fetch requests data from Netflix servers, and get data back and now cbF ready to be executed.
89+
**We have something called a Microtask Queue**
90+
- It is exactly same as Callback queue, but it has higher priority. Functions in Microtask Q are executed earlier than Callback Q.
91+
- cbF goes inside the Microtask Q and not callback Q. Once call stack is empty, Event loop gives chance for elements in **both** Microtask Queue and
92+
Callback Queue to enter Call Stack.
93+
94+
- In console, first *Start* and *End* are printed in console. First cbF goes in callstack and "CB Netflix" is printed. cbF popped from callstack
95+
- Next cbT is removed from callback Q, put in Call Stack, "CB Timeout" is printed, and cbT removed from callstack.
96+
97+
### What enters the Microtask Queue ?
98+
- All the callback functions that come through *promises* go in microtask Q.
99+
- **Mutation Observer :** Keeps on checking whether there is mutation in DOM tree or not, and if there, then it execeutes some callback function.
100+
- Callback functions that come through *promises and mutation observer* go inside Microtask Queue.
101+
- All the rest goes inside **Callback Queue aka. Task Queue**
102+
103+
- If the task in microtask Queue keeps creating new tasks in the queue, element in callback queue never gets chance to be run. This is called **starvation**
104+
105+
106+
107+
108+
109+
21110

22111

23112

0 commit comments

Comments
 (0)