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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
After each task, all waiting microtasks run before the next task. So this prints "promise" before "timeout":
A long loop on the stack blocks everything, because nothing can run until it finishes.