What Happens In The Event Loop When We Run Asynchronous Javascript Code?

In the previous post we saw a simple example of asynchronous javascript code. Let’s dive deeper into the subject and see what happens in the event loop in the case of asynchronous code.

Recap on the main difference between synchronous and asynchronous code:
When writing synchronous code:

  • We run one instruction after another

When running asynchronous code:

  • We allow asynchronous functions to run in the background (such as setTimeout() functions or functions which take a longer time to process)
  • We pass callbacks to these functions which run once the function finished whatever work it was doing
  • We continue with the execution of the code immediately. This is means that the code is non-blocking.

Basically we can use callback functions to push actions into the future so that the code becomes non-blocking.

But how does this work? Let me try to explain what happens in the event loop, and try some “art” while I’m at it.

What happens behind the scenes of javascript?

The event loop is part of what happens behind the scenes of javascript when functions and events (like DOM events) happen.

Let’s define the structure of the Javascript Runtime. The Javascript Runtime is where the code is executed when we run it.
The Javascript Runtime is made out of:

  1. The Execution Stack
  2. Web APIs
  3. The Message Queue
  4. The Event Loop

It is important to understand how each piece works and how they work together so we can execute asynchronous javascript.

Just a quick reminder, below there there is my asynchronous javascript code with which I will explain how the event loop works:

const two= () => {

const go = () => {


Here’s a representation of the Javascript Runtime with the execution stack, web APIs, the callback queue and the even loop. Let’s go through this code instruction by instruction and see how it looks like.

First up when the program initializes, the execution stack will contain only the global execution context.

javascript event loop how it works
At the begining of the program, we just have the initial phase setup.
The go function is called so it is added on the stack
Next, the first log call is added to the stack (logging 1)
After it is executed, the execution context of the first console.log call is popped from the stack.
The next instruction is added, a call to the two() function
The two function call the setTimeout() function which is added to the stack.
Here it gets interested because the setTimeout() will start a timer and pass a callback which is stored in the Web APIs.
Once the timer started and the callback was passed, the call to the setTimeout and consequently the call of the two() function are both popped from the stack.
While the timer is still running, the final call to the console.log from the go() function is added to the execution stack and sequentally executed. This is why the output is 1 3 so far, as the call to log 2 is still waiting for the timer to expire.
After the console.log(3) call is executed, it is popped from the stack. Since there are no more instructions in the go() function, the go() execution context is also popped from the execution stack.
Now that the timer has expired, the callback passed in the setTimeout() function is added to the callback queue first. It will wait there until the execution stack is empty to give it a chance to be moved to the stack.
Since the execution stack is now empty, the callback passed with the timer is placed on the execution stack
The call to console.log(2) is added to the stack and executed.
javascript event loop how it works
Afterwards the console.log(2) and the execution context of the callback passed by the timer are both popped from the stack and the code execution is finished.

This was a very basic example of how the event loop works with asynchronous code. If you want to know how more complex code would act, you can draw your own diagrams, but it will follow the same procedure, just at a different scale.

Leave a Reply