How does the JavaScript event loop work?
JavaScript runs your code on a single call stack. Asynchronous work such as timers, network requests and I/O is handled by the host environment, and its callbacks wait in queues. The event loop moves a callback onto the stack only when the stack is empty, always draining the microtask queue (promises) before taking the next macrotask (timers, events).