JavaScript Notes
Master JavaScript event loop interview questions with call stack, microtasks, macrotasks, Promise order, async/await execution, and real output prediction examples.
The event loop is always asked in senior JavaScript interviews. Understanding it separates juniors from seniors. Master this, and async JavaScript will never confuse you again.
Tips for Interview 💡
- Always say: *"The event loop picks from the microtask queue before the macrotask queue."*
Promise.resolve().then(...)= microtask (runs beforesetTimeout)setTimeout(fn, 0)= macrotask (runs after all Promises)async/awaitis just syntactic sugar over Promises — awaited code is microtask.- The call stack must be empty before the event loop picks new tasks.
Q1: The Classic Order Question
console.log("1");
setTimeout(function () {
console.log("2");
}, 0);
Promise.resolve().then(function () {
console.log("3");
});
console.log("4");1 4 3 2
Step-by-step:
console.log("1")→ sync → prints1setTimeout→ registered → goes to macrotask queuePromise.resolve().then(...)→ microtask scheduledconsole.log("4")→ sync → prints4- Stack empty → check microtask queue → prints
3 - Microtask queue empty → pick from macrotask → prints
2
Q2: Multiple Promises Order
start end A C B
Why? Both .then("A") and .then("C") are scheduled first. "B" is scheduled only after "A" resolves.
Q3: async/await Execution Order
async function foo() {
console.log("foo start");
const result = await Promise.resolve("awaited");
console.log(result);
console.log("foo end");
}
console.log("before");
foo();
console.log("after");before foo start after awaited foo end
Why? await suspends foo at that line, returns control to the caller (console.log("after")), then resumes as a microtask.
Q4: Nested Promises
1 3 2: nested
Why? Returning a Promise from .then adds two extra microtask hops. The "3" microtask sneaks in between "1" and "2".
Q5: setTimeout Inside Promise
sync 1 sync 2 promise 1 promise 2 timeout timeout inside promise
Q6: async/await vs Promise.then Order
async A sync E async B then C then D
Wait, let's be precise:
async A sync E then C async B then D
Note: await null schedules one microtask tick. then C was queued before asyncFunc was called in terms of the microtask order.
Q7: Promise.all Execution Order
start P1 created P2 created end All: ["P1", "P2"]
Note: Promise.all waits for ALL promises. Results are in input order, not resolution order.
Q8: Error in async/await
async function fetchUser() {
try {
const result = await Promise.reject(new Error("Not found"));
console.log(result);
} catch (err) {
console.log("Caught:", err.message);
}
console.log("After try/catch");
}
fetchUser();
console.log("Sync after call");Sync after call Caught: Not found After try/catch
Q9: Microtask Starvation
Microtasks done! setTimeout
Why? All microtasks (even recursively queued ones) are drained before any macrotask runs. setTimeout only runs after all microtasks complete.
Q10: queueMicrotask vs Promise
1 4 2 - queueMicrotask 3 - Promise
Both are microtasks — they run in the order they were queued.
Q11: setInterval and Clearing
Tick: 1 Tick: 2 Tick: 3 Cleared!
Q12: Async Iterator / for-await
1 2 3
Q13: Promise Constructor Executor
A B D C
Key: The Promise executor function runs synchronously. Only .then callbacks are async (microtasks).
Q14: Multiple awaits
1 4 2 5 3
Why? After each await, control returns to the event loop. Other microtasks (like "5") can run between awaits.
Q15: Event Loop + DOM Events
Common Traps 🪤
| Pattern | Queue Type | When Runs |
|---|---|---|
Promise.resolve().then(...) | Microtask | After current sync, before setTimeout |
setTimeout(fn, 0) | Macrotask | After ALL microtasks |
queueMicrotask(fn) | Microtask | After current sync, before setTimeout |
async/await continuation | Microtask | After current sync |
setInterval(fn, n) | Macrotask | Repeatedly, after delay |
Promise executor new Promise(fn) | Synchronous | Right now |
Interview Questions Quick Reference
| Q | Answer |
|---|---|
| What is the event loop? | Mechanism that moves tasks from queues to call stack when stack is empty |
| Microtask vs Macrotask? | Microtasks (Promises, queueMicrotask) run before macrotasks (setTimeout) |
Why setTimeout(fn, 0) isn't instant? | It's a macrotask — runs after all microtasks |
| Is Promise executor sync or async? | Synchronous — runs immediately |
| What drains first? | All microtasks → then ONE macrotask → all microtasks → ... |
Practice Self-Test
1 6 3 5 4 2
Summary
Event Loop Order:
1. Execute all synchronous code
2. Drain entire microtask queue (Promises, queueMicrotask)
3. Run ONE macrotask (setTimeout, setInterval, I/O)
4. Go to step 2Master this mental model and any output question becomes predictable!
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Top 25 JavaScript Event Loop Interview Questions & Answers 2026.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, interview, event, loop
Related JavaScript Master Course Topics