The Call Stack
Web APIs and Asynchronous Programming
This is where the Event Queue comes into play. A queue is a first in, first out data structure. You can think of it like the line at an amusement park. The first person in line is the first person to get on the ride. All of our asynchronous functions are added to the end of the Event Queue as they complete.
And now we have the glue which holds it all together, the Event Loop. The event loop is exactly what it sounds like, a loop! You can think of it as a continuously spinning ferris wheel. It’s a looping process that lets the Event Queue know when it’s safe to execute our asynchronous function in the main thread.
On each loop, the Event Loop does two things:
- It checks if there are Frames currently on the Call Stack
- It checks to see if there are any asynchronous functions waiting in the Event Queue.
Note: Node.js also has an event loop that works outside of the browser. More information here.