JavaScript Event Loop: Concurrency Model, Call Stack & Microtask Priority

The Event Loop is the foundational architectural component that allows single-threaded JavaScript to handle non-blocking, highly concurrent operations. If you understand the Event Loop, you understand the true runtime behavior of asynchronous JavaScript.

JavaScript Event Loop and Asynchronous Concurrency Model

Figure 14: JavaScript Concurrency Model: Call Stack, Web APIs, Microtask Queue (VIP), Macrotask Queue, and Event Loop.

The 4 Components of the Concurrency Model

  1. Call Stack: Single thread executing synchronous code frames in LIFO order.
  2. Web APIs / Node APIs: Background multi-threaded host workers managing timers, network fetch, and DOM events.
  3. Microtask Queue (VIP Priority): Houses Promise callbacks (.then, .catch, .finally), queueMicrotask(), and async/await continuations.
  4. Macrotask / Callback Queue: Houses setTimeout, setInterval, setImmediate, and I/O events.

The Classic Interview Puzzle (Dry-Run Execution)

Predict the exact output order of this script:

console.log("1. Synchronous Global Script");

setTimeout(() => {
    console.log("2. Inside setTimeout (Macrotask)");
}, 0);

Promise.resolve().then(() => {
    console.log("3. Inside Promise 1 (Microtask)");
}).then(() => {
    console.log("4. Inside Promise 2 (Microtask)");
});

console.log("5. End of Global Script");
Output OrderPrinted LineQueue Source & Mechanical Explanation
1st"1. Synchronous Global Script"Executed immediately in Call Stack.
2nd"5. End of Global Script"Synchronous execution completes; Call Stack becomes empty.
3rd"3. Inside Promise 1 (Microtask)"Event Loop drains Microtask Queue FIRST before any timer.
4th"4. Inside Promise 2 (Microtask)"Chained Promise microtask executed next in Microtask Queue.
5th"2. Inside setTimeout (Macrotask)"Microtask Queue is empty! Event Loop picks task from Macrotask Queue.

University Exam Corner (BCA Examination Notes)

The Golden Rule of Event Loop Priority:
Synchronous Code in Call Stack > Microtask Queue (Promises) > Macrotask Queue (Timers)

Next Step

How do large web applications split code across files using modules? Proceed to: JavaScript ES Modules (ESM: import & export) →

Explore Related JavaScript Tutorials

Accelerate your JavaScript mastery by studying these tightly connected core tutorials:

Async / Await Execution →

Pausing and resuming microtask functions.

Promises & Microtasks →

Microtask queue priority over macrotasks.

Timers & Macrotasks →

Browser timer queues in the event loop.

Execution Stack →

Call stack interactions with event loop.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!