GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing jsprep.pro — JavaScript Event Loop Interview Questions — JSPrep Pro The Mental ModelJavaScript is single-threaded — it can only do one thing at a time. But your browser handles timers, network requests, and user clicks simultaneously without freezing. How? Picture a restaurant with one chef (the JavaScript thread) and two order queues. Regular orders go in the main queue (macrotask queue). VIP rush orders go in a separate express lane (microtask queue). The chef always finishes the current dish completely. Then they check the express lane first — and keep serving VIP orders until the express lane is completely empty. Only then do they pick up the next regular order. That is the event loop. The three core participants: the call stack (the chef actively cooking), the microtask queue (the express VIP lane — drain completely after every task), and the macrotask queue (the regular queue — one task at a time). There are also Web APIs — the waiters taking orders out back. They handle timers, fetch, and events outside the JavaScript thread, and push callbacks into the queues when done. The critical rule many developers miss: microtasks drain completely before the engine even looks at the macrotask queue. One microtask can queue another microtask, which queues another — and none of the macrotasks run until all microtasks are done. The ExplanationThe four components of the event loop
The event loop algorithm — exactly, step by step
What goes where — the complete classification table
The fundamental rule — microtasks always win over macrotasks
Step-by-step trace:
Chained .then — understanding multi-tick microtask ordering
When a async/await — what it compiles to
The hardest event loop question — mixed macro and micro
Trace:
queueMicrotask — explicit microtask scheduling
Starving the macrotask queue — infinite microtask recursion
This freezes the browser (or hangs the Node process). No user interactions, no renders, no macrotasks — ever. This is why infinite async recursion should use
requestAnimationFrame — the rendering checkpoint
rAF callbacks execute at the start of each browser frame (after microtasks drain, before the frame is painted). They are not macrotasks — they have their own animation frame callback queue. The precise order each frame: macrotask → drain microtasks → rAF callbacks → paint. Using rAF for animations ensures they are synchronised to the display refresh cycle. setTimeout is not synchronised — it causes jank. The 60fps budget — JavaScript blocks renderingThe browser targets 60 frames per second = one frame every 16.67ms. Within each frame: JavaScript runs → style → layout → paint → composite. If JavaScript alone takes longer than ~10ms, the frame budget is blown and the browser drops a frame. Tasks longer than 50ms are classified as Long Tasks and appear as jank.
setTimeout — delay is a minimum, not a guarantee
setTimeout 0ms fires as fast as the event loop allows — after the current call stack empties and all microtasks drain. If a synchronous function takes 500ms, your setTimeout(fn, 0) fires 500ms+ later. setInterval drift and the fix
Promise.all — what actually happens in the event loop
Promise.all does NOT queue promises sequentially. The async operations (fetch calls) all start in the Web API layer simultaneously. JavaScript yields at await. As each response arrives, its resolve callback is queued. When the last one resolves, Promise.all resolves — and the continuation is a microtask. Sequential vs parallel async — the most costly interview bug
The sequential pattern in Node.js event loop — phases (different from browser)Node's event loop has explicit phases powered by libuv:
Between every phase, Node drains the microtask queue (Promise.then) AND the nextTick queue (process.nextTick — which fires BEFORE Promise microtasks). Node.js: process.nextTick vs Promise.then vs setImmediate
Web Workers — truly parallel event loops
Workers have their own call stack, event loop, and memory. They cannot access the DOM. Communication via Common Misconceptions
Where You’ll See This in Real Code
Interview Cheat Sheet
How to Answer in an Interview
Deep Dive ArticlesPromise.all vs allSettled vs race vs any: The Complete Comparison9 min readJavaScript Promises & Async/Await: The Complete Mental Model12 min readJavaScript Prototypes Explained: The Object Model Behind Every Class and Method10 min readJavaScript Performance Optimization: What Actually Makes Code Fast12 min readJavaScript Event Loop Explained: Call Stack, Microtasks, and Macrotasks11 min readTop 50 JavaScript Interview Questions (With Deep Answers)18 min readJavaScript Output Questions: 30 Tricky Examples That Test Real Understanding14 min read Practice Questions13 questions#01 Explain the event loop, call stack, and microtask queue. EasyCore JSSynchronous → Microtasks (all) → Next Macrotask #02 EasyEvent Loop & Promises Missing return in Promise chain drops value #03 MediumEvent Loop & Promises Sequential awaits for independent operations #04 Synchronous code runs before setTimeout EasyEvent Loop & Promises #05 Promise microtask before setTimeout macrotask MediumEvent Loop & Promises #06 Nested setTimeout creates new macrotask HardEvent Loop & Promises #07 Promise.then is always async — even when resolved MediumEvent Loop & Promises #08 queueMicrotask runs before setTimeout MediumEvent Loop & Promises #09 throw inside .then becomes rejection, caught by .catch HardEvent Loop & Promises #10 What is queueMicrotask() and when should you use it? EasyAsync JSSchedule a function in the microtask queue — runs after current sync, before next macrotask #11 What are Web Workers and when should you use them? MediumBrowser APIsRun JS in a background thread — no DOM access; use postMessage to communicate #12 What is the rendering phase of the browser event loop? MediumCore JSBetween macrotasks: style → layout → paint → compositing — sync code blocks it #13 async try/catch catches awaited rejection HardEvent Loop & Promises Related TopicsCan you answer these under pressure?Reading answers is not the same as knowing them. Practice saying them out loud with AI feedback: that’s what builds real interview confidence. | ||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |