23/06/2026
π Daily Coding Series by Waheed β Day 81
JavaScript Event Loop β The Mechanism Behind Everything
( Note:After a brief pause, the series is back.
Let us pick up exactly where we left off.)
One of the most confusing moments in JavaScript is this:
π You write a fetch call
π Directly below it you write console.log("done")
π You run the code
π The log prints before the data arrives π
At first, it feels like a bug.
But in reality, it is the Event Loop doing its job.
πΉ What is the Event Loop
The Event Loop is the mechanism that manages how JavaScript handles tasks that take time β fetch calls, timers, event listeners β without freezing the entire page.
πΉ Why it matters
JavaScript runs on a single thread. One operation at a time. When it hits a fetch call or a timer, it hands that work to the browser and moves on immediately. It does not wait.
πΉ How it works
The Call Stack holds what is currently running
The Web APIs handle work that takes time
The Callback Queue holds results that are ready
The Event Loop pushes results to the stack when it is empty
πΉ The detail most people miss
setTimeout zero milliseconds does not mean now.
It means after the current stack is empty.
Zero is a minimum. Not a guarantee.
The Event Loop decides when β not the timer.
πΉ Where Promises fit in
Promises sit in the Microtask Queue β not the regular Callback Queue. The Event Loop always clears Microtasks before Callbacks. That is why Promise results arrive before setTimeout results.
πΉ Key takeaway
The Event Loop is not an advanced topic. It is the foundation. Every concept from Day 74 to today β Callbacks, Promises, Async/Await β all of it runs through the Event Loop.
π Next β Day 82
π The Call Stack
π¬ Comment DAY 81 DONE
π Follow Muhammad Waheed Asghar for daily JavaScript breakdowns