Event Loop (JavaScript)
The event loop is the mechanism that lets single-threaded JavaScript handle asynchronous tasks. It runs queued callbacks once the main code has finished.
JavaScript
JavaScript runs one piece of code at a time. When async work like a timer or network request completes, its callback waits in a queue. The event loop pushes that callback onto the call stack only when the stack is empty.
This is why code after a setTimeout, or after an awaited promise, runs later even with a 0ms delay.
Examples
JavaScript
console.log("start");
setTimeout(() => console.log("later"), 0);
console.log("end");Output:
start
end
later