Glossary

Event Loop

Event Loop is the mechanism for executing asynchronous code in single-threaded environments (JavaScript, Node.js). It allows handling thousands of concurrent connections without threads by offloading I/O operations to the OS.

How it works

  1. Call Stack — synchronous code executes sequentially
  2. When an async operation is encountered (HTTP request, setTimeout) — it is handed to Web API/libuv and runs outside the main thread
  3. When the operation completes — the callback enters the Callback Queue
  4. The Event Loop checks: if the Call Stack is empty — it moves the callback onto the stack

Macrotasks vs Microtasks

Microtasks (Promise.then, queueMicrotask) execute before the next macrotask (setTimeout, setInterval). So Promise.resolve().then(fn) runs before setTimeout(fn, 0).