JavaScript is single-threaded. It has one Call Stack. The Event Loop is the traffic controller that decides what goes onto that stack.
The Priority Lane (Microtasks)
Not all tasks are equal.
• MacroTasks (setTimeout, setInterval): Regular lane.
• MicroTasks (Promises, QueueMicrotask): VIP Lane.
The Event Loop always empties the VIP lane (Promises) before processing the next regular car (setTimeout).
The Ultimate Quiz
Predict the order before running this. This is a very common interview question.
javascript
console.log("1. Start");
setTimeout(() => {
console.log("2. Timeout (Macro)");
}, 0);
Promise.resolve().then(() => {
console.log("3. Promise (Micro)");
});
console.log("4. End");
/*
Order Logic:
1. "Start" (Synchronous)
2. "End" (Synchronous)
3. "Promise" (Microtask Queue - Higher Priority)
4. "Timeout" (Macrotask Queue - Lower Priority)
*/