The event loop lets JavaScript handle async tasks on a single thread.
Example Code#
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
Promise.resolve().then(() => {
console.log("Promise");
});
console.log("End");
Output: Start → End → Promise → Timeout
Visual Timeline (SVG)#
Event Loop SVG
Why this order?#
StartandEndare synchronous → run immediately on the call stack.Promise.thenis a microtask → runs before macrotasks.setTimeoutcallback is a macrotask → runs after microtasks drain.
Start the discussion
Questions, corrections, a better approach? I read and reply to every comment.