4.3 Call Stack & Event Loop

We are going to answer two questions:

  1. why does the 2-seconds timer not prevent the program from running?
  2. why does the 0-second timer get delayed after the line below it gets executed?

Call Stack

For asynchronous program, the only thing you need to worry about is the call stack. We are not using Node APIs or Callback Queue.
The Call Stack is the basic data structure that you can add item to the top and remove item from the top too.
Illustration of Call Stack

Add main()

Add var x = 1; to the top.

Remove var x = 1; from the top.

Add to the top

Remove from the top

Add to the top

Remove from the top

Remove main


console.log('Starting app');
setTimeout(() => {
console.log("Inside of Callback");
}, 2000);
setTimeout(() => {
console.log("Inside of Callback 2");
}, 0);
console.log('Finishing up');

The asynchronous part works as illustrated above.
Let’s talk about the synchronous part.