Stack, queue, and Web APIs

So what happened when we called setTimeout() in the previous code? Where did it magically disappear from the call stack, making room for the next function execution?

Well, setTimeout() is a web API provided by each browser individually. When you call setTimeout, the call stack sends the setTimeout() function call to the Web API, which then keeps track of the timer (in our case) till it's complete.

Once the Web API realizes the timer is complete, it does not immediately push the contents back to the stack. It pushes a callback of the  setTimeout() function to something known as a queue. A Queue, as the name implies, could be a queue of functions to be executed.

This is when the event loop comes into play. The event ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.