The JavaScript event loop is the mechanism that decides what runs next in your program. Your code runs on a single call stack, and when that stack becomes empty, the runtime picks up the callbacks that are waiting in line. It always checks the microtask queue first and the task queue second. Once you can picture those three parts clearly, the order of promises, timers, and await stops being surprising and starts being predictable. The previous lesson in this module covered the promises that feed those queues, and the next lesson looks at how you chain them together.
What are the moving parts of the event loop?
There are three of them, and keeping them separate is the whole trick.
- The call stack holds the function calls that are currently in progress. Your script starts on the stack and stays there until the last statement finishes.
- The microtask queue holds callbacks that must run before anything else. Promise reactions and
queueMicrotaskcalls go here. - The task queue, which is sometimes called the macrotask queue, holds callbacks that come from timers, I/O events, and other browser APIs.
When the call stack becomes empty, the runtime checks the microtask queue. If that queue holds anything, the entire queue is drained, and that includes any callbacks that those callbacks add, before the runtime looks at the task queue again. Only then does one single task run, and the whole cycle starts again from the top.
Why does a resolved promise run before a zero-delay timer?
Because setTimeout(fn, 0) does not mean "run this right away". It means "put this in the task queue and run it during a later turn of the loop". A promise callback goes into the microtask queue, and microtasks always drain before the next task is chosen. That is why even a timer with a delay of zero loses to a promise that has already been resolved.
console.log('1 sync start');
setTimeout(function () {
console.log('2 timer callback');
}, 0);
Promise.resolve().then(function () {
console.log('3 promise callback');
});
queueMicrotask(function () {
console.log('4 queued microtask');
});
console.log('5 sync end');
The output is always the same:
1 sync start
5 sync end
3 promise callback
4 queued microtask
2 timer callback
Everything up to 5 sync end is ordinary synchronous code that runs on the stack. Then the stack empties, both microtasks run in the order in which they were queued, and only after that does the timer callback get its turn.
What does the order actually look like with real timings?
The performance.now() function gives you a millisecond timer with sub-millisecond precision, which is enough to see the difference between the two queues in a real run.
const start = performance.now();
function at(label) {
const elapsed = (performance.now() - start).toFixed(1);
console.log(label + ' at ' + elapsed + 'ms');
}
at('script start');
setTimeout(function () {
at('timer 0ms');
}, 0);
Promise.resolve().then(function () {
at('promise callback');
});
queueMicrotask(function () {
at('microtask callback');
});
at('script end');
A typical run prints the script start and the script end inside the same millisecond. The two microtasks appear a fraction of a millisecond later, and the timer appears a little after that. The exact numbers change on every machine and on every run, and the very first call to performance.now() in a fresh process can even include the cost of loading the page. The order is the reliable part, and the number of milliseconds is not.
Where does await fit into the queues?
An await splits an async function into two halves. The code before the await runs synchronously, exactly like any other function would. The code after it is scheduled as a microtask, so it resumes on a later turn of the loop.
async function work() {
console.log('a: inside, before await');
await null;
console.log('c: inside, after await');
}
console.log('b: outside, before work()');
work();
console.log('d: outside, after work()');
The output is b, a, d, and then c. Calling an async function runs its body immediately up to the first await, so a prints before d even though the function was called first. The await null suspends the function and hands control back to the caller, and the rest of the function resumes later as a microtask.
What happens when your code blocks the event loop?
Nothing else at all can run while the call stack is busy. A timer that was scheduled for zero milliseconds still has to wait for whatever synchronous work is in front of it, and a mouse click waits for exactly the same reason. This is the most common cause of a page that seems to freeze for a moment and then suddenly processes a whole queue of clicks at once.
const start = Date.now();
setTimeout(function () {
console.log('timer fired after ' + (Date.now() - start) + 'ms');
}, 0);
const until = Date.now() + 200;
while (Date.now() < until) {
// This stands in for a long synchronous calculation.
}
console.log('busy work finished');
The busy loop runs to completion first, and then the timer fires roughly 200 milliseconds late instead of immediately. Animations that are driven by requestAnimationFrame stutter for the same reason. If a piece of work is genuinely heavy, such as parsing a huge file or sorting hundreds of thousands of records, move it off the main thread with a worker, or break it into smaller chunks that give the browser a chance to breathe between the pieces.
What to remember about the event loop
- Synchronous code runs on the call stack, and it blocks everything else until it finishes.
- When the stack empties, the whole microtask queue drains before the next task runs.
- Promise callbacks and
queueMicrotaskare microtasks, while timers and I/O callbacks are tasks. - A zero-millisecond timer still runs after every pending microtask.
awaitresumes an async function as a microtask, so the rest of the function always happens later than the call that started it.- Long synchronous work delays timers, clicks, and animations in the same way.
Try it yourself: Run the first block and confirm the printed order. Then move the setTimeout line to the very top of the file and run it again. The order stays the same, because a timer only cares about when it fires and not about where you wrote it. Finally, change the 200 in the busy loop to 1000 and watch the timer report a delay of about a second. That is the event loop showing you exactly what your synchronous code costs everybody else.