-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_loop.js
More file actions
89 lines (71 loc) · 1.55 KB
/
event_loop.js
File metadata and controls
89 lines (71 loc) · 1.55 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// sync in js
// | |
// | |
// | |
// | |
// | |
// | | => when func first call -> first call push in stack
// | First | and execute
// |________|
// Call Stack
// first = () => {
// console.log("First")
// }
// first();
// | |
// | |
// | |
// | |
// | |
// | | => After func first call finished then Final push in to stack
// | Final | and execute
// |________|
// Call Stack
// console.log('Final')
// Output
// First
// Final
// Example Event loop multi thread with setTimeout () ansync in marco task in Event loop
console.log("First !")
// Function second() call after x000000 mili s
setTimeout(function second(){
console.log('Time out !')
}, 00000)
// | |
// | |
// | |
// | |
// | |
// | | => when func first call -> first call push in stack
// | First | and execute
// |________|
// Call Stack
// | |
// | |
// | |
// | |
// | |
// | | => when func finished -> second push into stack and encounter time
// | second | push event queue
// |________|
// Call Stack
// _________________________
// |
// second |
// ________|________________
// Event queue
// | |
// | |
// | |
// | |
// | |
// | | => Final push into call stack no need second exits
// | Final |
// |________|
// Call Stack
// Then -> Event Queue push into stack -> print (Timeout)
console.log("Final !")
//Output print
// First !
// Final !
// Time out !