How the JavaScript Event Loop Works, Explained by a Busy Café

ou’re next in line at a café. The barista starts an espresso, turns to you, and takes your order while the shot pours. Now imagine the same barista counting every coffee bean in the shop by hand. The espresso machine can still finish, and the doorbell can still ring, but nobody at the counter can answer “Could I get that iced?”

A web page can feel like either café. It can stay responsive through a two-second network request, then freeze during a calculation that takes half a second. The difference is what the page’s main thread is doing when you need its attention. MDN

The call stack is the barista’s counter

Say you tap Add iced latte in the café’s ordering app. The function handling your tap calls another function to calculate the price. That function calls a third to look up the oat milk surcharge. JavaScript keeps track of these unfinished calls on the call stack. The most recent call sits on top. It returns the surcharge, the price calculation finishes, and the original tap handler can update your basket. MDN

It’s one order with several steps. If you tap Checkout halfway through the calculation, the code handling your new tap has to wait. For a little arithmetic, you’ll barely notice. If adding a latte also recalculates the total for every order the café has taken this month, you might tap Checkout twice because the first tap appeared to do nothing. MDN

Who watches the espresso machine?

Now the app needs to ask the server which pickup times are available. It starts a network request with fetch(). The browser manages the request, leaving the main thread available for other work. If the ordering function uses await, that function pauses at the point where it needs the answer. Meanwhile, you can still open the menu or change your drink size. MDN

When the answer arrives, the rest of the function becomes eligible to run. Think of the espresso machine beeping. The drink is ready, but if the barista is counting change, they finish before picking up the cup. In the browser, the event loop coordinates when ready work gets a turn on the main thread. That’s why a server can reply promptly and the page can still take a moment to show the pickup times. MDN

Which order comes next?

Here’s a small experiment. Read these four lines and guess which message appears last. Then run them together in your browser console.

console.log("Barista takes the order");
setTimeout(() => console.log("Timer rings"), 0);
Promise.resolve().then(() => console.log("Payment confirmed"));
console.log("Barista puts down the pen");
OrderMessageWhy it appears here
1Barista takes the orderThe first line runs immediately.
2Barista puts down the penThe script continues after scheduling the two callbacks.
3Payment confirmedThe promise is already fulfilled, so its callback is waiting in the microtask queue.
4Timer ringsThe timer callback gets a later turn, after the waiting microtask.

The timer was registered first and given a delay of zero. So how did it come last? A zero-delay timer asks for a future turn. The promise callback gets a different kind of turn, called a microtask. When the current task finishes, the browser runs waiting microtasks before selecting another task. It drains that queue even if a microtask adds another one. A long chain can therefore keep a customer’s click waiting and delay the next screen update. MDN

Why “Preparing your report…” sometimes never appears

Picture the café manager clicking Export sales report. The app changes the button text to Preparing your report…, calculates totals across 80,000 orders, and changes the text again to Download report. All of that happens inside one click handler. On screen, the button seems stuck on Export and then jumps straight to Download. Where did the middle message go?

The code changed the text twice. The browser only got a chance to paint the result after the calculation finished. This is one reason developers look for long tasks when investigating a frozen interface. In a browser performance trace, that term means a main-thread task lasting longer than 50 milliseconds. A customer experiences it more simply. They tap a button and wonder whether the app heard them. MDN

For this report, one fix is to process orders in batches and yield between them. Those breaks give the browser a chance to display progress and handle a click on Cancel. For a calculation that takes sustained CPU time, a Web Worker can compute the totals on a separate thread and send the result back. Simply declaring the export function async leaves the calculation on the same thread. A practical way to test the improvement is to start the export and immediately try to cancel it. Does the button respond while the report is still being prepared? web.dev

Conclusion

A good barista can take your order while the espresso pours. A responsive page can do the same while a server answers. The trouble starts when one job occupies the counter so long that every small request piles up behind it. When a page feels frozen, ask “What was the main thread doing when I clicked?” The answer can show you where to shorten a calculation, split it into turns, or hand it to a worker so the next customer can be served.