← ByteDance Interview Insights
I knew the rough shape of this answer but fumbled the ordering details under pressure.
Start by defining the event loop as the mechanism that coordinates the call stack, macrotask queue, and microtask queue to enable non-blocking asynchronous behavior in JavaScript. Then walk through the execution order: synchronous code runs first, then all microtasks are drained, then one macrotask is processed, repeating the cycle. Use a concrete example like setTimeout vs Promise to illustrate the priority and timing differences.
Pro tip: Emphasize that microtasks are processed completely before the next macrotask, and mention that this can lead to starvation if microtasks keep scheduling more microtasks. Also note that rendering happens between macrotasks, not microtasks, which is crucial for UI performance.
Explain that the event loop is a continuous process that monitors the call stack and task queues. Introduce the call stack (where synchronous code executes), macrotask queue (e.g., setTimeout, setInterval, I/O), and microtask queue (e.g., Promises, MutationObserver).
Detail the cycle: run all synchronous code on the call stack until empty, then drain the entire microtask queue, then take one macrotask from the queue and run it, then drain microtasks again, and repeat. Mention that rendering occurs between macrotasks.
Provide a code snippet showing the output order of console.log, setTimeout, and Promise.resolve().then. Walk through step-by-step how the event loop processes each, highlighting why microtasks run before the next macrotask.
Explain how this architecture enables non-blocking I/O but can cause issues like microtask starvation or delayed rendering if misused. Mention that long-running synchronous code blocks the event loop entirely.
Tie back to frontend performance: how understanding the event loop helps optimize rendering, avoid jank, and debug async issues. Mention tools like Chrome DevTools Performance panel for observing task queues.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
First, clearly state the event loop model: synchronous code runs first, then microtasks (Promise callbacks), then macrotasks (setTimeout). Then walk through the snippet line by line, categorizing each operation and predicting the output order. Finally, explain why the order occurs, referencing the event loop phases.
Pro tip: Mention that the exact order can depend on the environment (browser vs Node.js) and the presence of other tasks, but the fundamental microtask-before-macrotask rule holds. Also, note that Promise.then callbacks are microtasks, while setTimeout callbacks are macrotasks.
Scan the snippet for any code that executes immediately, such as console.log statements or function calls not wrapped in callbacks. These run first, in order.
Look for Promise.then, queueMicrotask, or other microtask sources. These are queued and run after the current synchronous code completes, before any macrotasks.
Look for setTimeout, setInterval, or other macrotask sources. These are queued and run after all microtasks have been processed.
Walk through the code from top to bottom, noting when each callback is scheduled and when it will execute based on the event loop rules.
State the exact sequence of printed messages, and briefly explain why each occurs when it does, referencing the event loop phases.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Answered that await essentially pauses execution and resumes via a microtask, similar to a .then callback.
Start by clarifying that async/await is syntactic sugar over Promises, then walk through the transformation: an async function returns a Promise, and each await pauses execution by yielding control back to the event loop. Explain the runtime mechanics—how await wraps the expression in a Promise.resolve, suspends the async function, and schedules its continuation as a microtask once the awaited Promise settles.
Pro tip: Mention that await always defers continuation to a microtask, even if the Promise is already resolved, which explains why async functions never block and why ordering matters in microtask queues. Also note that async/await doesn't replace Promises—it uses them, so understanding Promise internals is still essential for debugging and performance.
State that async/await is built on Promises: an async function always returns a Promise, and await is a syntactic way to consume a Promise. This sets the foundation for the deeper explanation.
Describe how the runtime (or transpiler) transforms await into Promise.resolve(expression).then(continuation). The async function's body after await becomes the callback passed to then.
Explain that when await is hit, the async function suspends, returning control to the caller and the event loop. The continuation is scheduled as a microtask when the awaited Promise settles.
Emphasize that even if the Promise is already resolved, the continuation runs asynchronously in a microtask, not synchronously. This ensures non-blocking behavior and consistent ordering.
Tie it back to real-world impact: error handling via try/catch, debugging async stack traces, and performance considerations like avoiding unnecessary awaits in loops.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.