-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrotaskQueue.js
More file actions
65 lines (49 loc) · 1.89 KB
/
Copy pathmicrotaskQueue.js
File metadata and controls
65 lines (49 loc) · 1.89 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
56
57
58
59
60
61
62
63
64
65
/*
Microtask queue is given priority over any other queues by Event loop
Event loop will start processing any other queue only after it is done processing all items of Microtask queue (i.e. till Microtask queue is not empty)
Whenever event loop reaches microtask queue (start processing micro task queue) it will not move to any other thing till Microtask queue is not empty
*/
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
queueMicrotask(() => console.log('4'));
async function func() {
console.log('5');
await null;
console.log('6');
}
await func();
console.log('7');
/*
1 is consoled
2 goes to macrotask queue
3 goes to microtask queue
4 goes to microtask queue
now at line await func(), await is used so everything after that become the callback of the func promise i.e.
func().then(() => {
all code after it
})
execution will go inside func console 5
await null will run immediately and the next line after it becomes the callback of the await null promise and considered as callback atatched to then (same logic as above) and will get added in microtask queue [ 3, 4, 6 ]
there is no synchornous code left i.e. call stack is empty to event loop process (drains) microtask queue
and prints 3, 4, 6
no func() promise finishes and the its then runs i.e.
func().then(() => {
console.log('7');
})
so 7 is pushed to microtask queue [ 7 ]
event loop again goes to microtask queue and prints 7
then goes to macrotask queue and prints 2
*/
// above code is similar to
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
queueMicrotask(() => console.log('4'));
function func() {
console.log('5');
return Promise.resolve().then(() => console.log('6'));
}
func().then(() => {
console.log('7');
});