---
title: "How does the JavaScript event loop work?"  
description: "How does the JavaScript event loop work?"  
author: "Eetu Saarinen"  
published: 2026-11-13  
updated: 2026-11-13  
canonical: https://www.mindstick.com/forum/163011/how-does-the-javascript-event-loop-work  
category: "JavaScript and Frontend"  
tags: ["javascript", "Event Loop", "Async", "call stack", "Promises"]  
reading_time: 1 minute  

---

# How does the JavaScript event loop work?

JavaScript is single-threaded, yet setTimeout and fetch do not block the page. How is that possible?

## Replies

### Reply by Vaibhav Kumar

JavaScript runs your code on a single call stack, one function at a time. Slow things like timers and network requests are handled by the browser (or Node.js) outside that stack. When one finishes, its callback is placed in a queue. The **event loop** keeps checking: if the call stack is empty, it takes the next callback from a queue and runs it.

There are two main queues:

- **Microtask queue**: promise callbacks (.then, await continuations) and queueMicrotask.
- **Task (macrotask) queue**: setTimeout, setInterval, events.

After each task, all waiting microtasks run before the next task. So this prints "promise" before "timeout":

```
setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
```

A long loop on the stack blocks everything, because nothing can run until it finishes.


---

Original Source: https://www.mindstick.com/forum/163011/how-does-the-javascript-event-loop-work

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
