Interview Q&A · JavaScript · 16 questions in this topic

Event Loop, Promises & Async/Await — JavaScript Interview Q&A

All JavaScript topics

The async core — queues, promise machinery, parallel patterns and cancellation. Part of Async JavaScript · 204 questions in the JavaScript bank.

How does the event loop work (stack, queues, render)?

Answer

The stack runs one frame at a time; async completions queue as microtasks (promises, queueMicrotask — drained fully after each task) or macrotasks (timers, I/O, events — one per turn); rendering typically slots between macrotasks. Starving microtasks (promise loops) freeze rendering — the loop has no preemption.

Stack-empty → microtasks-all → one-macrotask → render is the turn anatomy.

Key points

  1. 1Stack — synchronous execution.
  2. 2Microtasks — promises, drained fully.
  3. 3Macrotasks — timers/I/O, one per turn.

Example

Example.jsjs
setTimeout(() => console.log('t'), 0); // macrotask
Promise.resolve().then(() => console.log('p')); // microtask: first

💡 Microtasks-before-macrotasks explains every ordering puzzle.

What is a Promise (states, executor, then/catch)?

Answer

A promise is a future-value placeholder: pending → fulfilled/rejected (settled once, immutable after). The executor runs immediately (sync!) with resolve/reject; then/catch/finally register reactions returning new promises (chainable). Unhandled rejections crash modern runtimes — always terminate chains with catch.

Executor-sync, settlement-once, chains-composed is the model.

Example

Example.jsjs
const p = new Promise((resolve) => {
  // executor runs NOW (synchronously)
  resolve(42);
});
p.then((v) => v * 2).then(console.log); // 42, then 84

How does promise chaining work (returning values vs promises)?

Answer

then returns a new promise: returning a value fulfills it (assimilation), returning a promise adopts its fate (flattening — no nested promise pyramids), throwing rejects it. Forgetting return passes undefined downstream — the silent chain-breaker.

Return-or-adopt is the chaining law; missing-return is the bug.

Example

Example.jsjs
fetchUser(id)
  .then((u) => fetchOrders(u.id)) // returned promise adopted
  .then((orders) => render(orders))
  .catch(showError); // any step's failure lands here

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits