Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. # Regular Event Loop
  2.  
  3. This shows the execution order given JavaScript's Call Stack, Event Loop, and
  4. any asynchronous APIs provided in the JS execution environment (in this example;
  5. Web APIs in a Browser environment)
  6.  
  7. ---
  8.  
  9. Given the code
  10.  
  11. ```javascript
  12. setTimeout(() => {
  13. console.log('hi')
  14. }, 1000)
  15. ```
  16.  
  17. The Call Stack, Event Loop, and Web APIs have the following relationship
  18.  
  19. ```text
  20. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  21. --------------------|-------------------|--------------| |---------------|
  22. setTimeout(() => { | | | | |
  23. console.log('hi') | | | | |
  24. }, 1000) | | | | |
  25. | | | | |
  26. ```
  27. To start, everything is empty
  28.  
  29. ---
  30.  
  31. ```text
  32. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  33. --------------------|-------------------|--------------| |---------------|
  34. setTimeout(() => { | <global> | | | |
  35. console.log('hi') | | | | |
  36. }, 1000) | | | | |
  37. | | | | |
  38. ```
  39. It starts executing the code, and pushes that fact onto the Call Stack (here named
  40. `<global>`)
  41.  
  42. ---
  43.  
  44. ```text
  45. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  46. --------------------|-------------------|--------------| |---------------|
  47. > setTimeout(() => { | <global> | | | |
  48. console.log('hi') | setTimeout | | | |
  49. }, 1000) | | | | |
  50. | | | | |
  51. ```
  52. Then the first line is executed. This pushes the function execution as the
  53. second item onto the call stack.
  54.  
  55. Note that the Call Stack is a _stack_; The last item pushed on is the first
  56. item popped off. Aka: Last In, First Out. (think; a stack of dishes)
  57.  
  58. ---
  59.  
  60. ```text
  61. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  62. --------------------|-------------------|--------------| |---------------|
  63. > setTimeout(() => { | <global> | | | timeout, 1000 |
  64. console.log('hi') | setTimeout | | | |
  65. }, 1000) | | | | |
  66. | | | | |
  67. ```
  68. Executing `setTimeout` actually calls out to code that is _not_ part of JS.
  69. It's part of a _Web API_ which the browser provides for us.
  70. There are a different set of APIs like this available in node.
  71.  
  72. ---
  73.  
  74. ```text
  75. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  76. --------------------|-------------------|--------------| |---------------|
  77. setTimeout(() => { | <global> | | | timeout, 1000 |
  78. console.log('hi') | | | | |
  79. }, 1000) | | | | |
  80. | | | | |
  81. ```
  82. `setTimeout` is then finished executing; it has offloaded its work to the Web
  83. API which will wait for the requested amount of time (1000ms).
  84.  
  85. ---
  86.  
  87. ```text
  88. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  89. --------------------|-------------------|--------------| |---------------|
  90. setTimeout(() => { | | | | timeout, 1000 |
  91. console.log('hi') | | | | |
  92. }, 1000) | | | | |
  93. | | | | |
  94. ```
  95.  
  96. As there are no more lines of JS to execute, the Call Stack is now empty.
  97.  
  98. ---
  99.  
  100. ```text
  101. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  102. --------------------|-------------------|--------------| |---------------|
  103. setTimeout(() => { | | function <-----timeout, 1000 |
  104. console.log('hi') | | | | |
  105. }, 1000) | | | | |
  106. | | | | |
  107. ```
  108. Once the timeout has expired, the Web API lets JS know by adding code to the
  109. Event Loop.
  110.  
  111. It doesn't push onto the Call Stack directly as that could intefere with already
  112. executing code, and you'd end up in weird situations.
  113.  
  114. The Event Loop is a _Queue_. The first item pushed on is the first
  115. item popped off. Aka: First In, First Out. (think; a queue for a movie)
  116.  
  117. ---
  118.  
  119. ```text
  120. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  121. --------------------|-------------------|--------------| |---------------|
  122. setTimeout(() => { | function <---function | | |
  123. console.log('hi') | | | | |
  124. }, 1000) | | | | |
  125. | | | | |
  126. ```
  127. Whenever the Call Stack is empty, the JS execution environment occasionally checks
  128. to see if anything is Queued in the Event Loop. If it is, the first item is moved
  129. to the Call Stack for execution.
  130.  
  131. ---
  132.  
  133. ```text
  134. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  135. --------------------|-------------------|--------------| |---------------|
  136. setTimeout(() => { | function | | | |
  137. > console.log('hi') | console.log | | | |
  138. }, 1000) | | | | |
  139. | | | | |
  140. ```
  141. Executing the function results in `console.log` being called, also pushed onto
  142. the Call Stack.
  143.  
  144. ---
  145.  
  146. ```text
  147. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  148. --------------------|-------------------|--------------| |---------------|
  149. setTimeout(() => { | function | | | |
  150. console.log('hi') | | | | |
  151. }, 1000) | | | | |
  152. | | | | |
  153. > hi
  154. ```
  155. Once finished executing, `hi` is printed, and `console.log` is removed from the
  156. Call Stack.
  157.  
  158. ---
  159.  
  160. ```text
  161. [code] | [call stack] | [Event Loop] | | [Web APIs] |
  162. --------------------|-------------------|--------------| |---------------|
  163. setTimeout(() => { | | | | |
  164. console.log('hi') | | | | |
  165. }, 1000) | | | | |
  166. | | | | |
  167. > hi
  168. ```
  169. Finally, the function has no other commands to execute, so it too is taken off
  170. the Call Stack.
  171.  
  172. Our program has now finished execution.
  173.  
  174. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement