Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 77 ยท JavaScript Interview Questions

JavaScript: Promises, async/await and the Event Loop

Predict output order with microtasks and macrotasks, handle promise errors, and compare Promise.all, allSettled, race and any, with runnable interview examples.

"What does this code print?" followed by a snippet with setTimeout, a promise and a few console.log calls is probably the most common JavaScript interview puzzle. It is not a trick. It tests whether you know one rule: JavaScript runs one piece of code at a time, and finished work waits in queues in a fixed priority order.

This lesson gives you that rule, a method for solving ordering puzzles step by step, and short answers for error handling and the Promise combinators. For the full teaching versions see promises, async/await and promise combinators.

The event loop in one picture

Who runs when

Call stack Runs the current code to the end.
Microtasks Promise callbacks. Drained completely first.
Macrotasks Timers, events, I/O. One per turn.

After each macrotask, all microtasks run before the next one.

1 / 2
flowchart TD
  A["Run script (a macrotask)"] --> B["Run ALL microtasks"]
  B --> C["Render (browser only)"]
  C --> D["Take next macrotask"]
  D --> B

After every macrotask, the microtask queue is emptied completely.

The synchronous code of your script runs first. Then every queued microtask runs, including microtasks added by other microtasks. Only then does the loop take the next timer or event callback. This is why setTimeout(fn, 0) runs after a resolved promise's .then, even though the timeout was registered first.

Solving an ordering puzzle

Use four steps, and say them aloud:

  1. Run all synchronous code top to bottom. A promise executor runs synchronously too.
  2. Whenever a promise is settled, its .then callbacks go on the microtask queue. An await queues the rest of the function.
  3. Whenever setTimeout is called, its callback goes on the macrotask queue.
  4. After the sync code ends, drain microtasks in order, then take the next macrotask.
JS Classic ordering puzzle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Walk through it: the sync lines print first (1, the executor line, script end). Then the microtasks in the order they were queued: "then A", the plain microtask, and only after "then A" finishes does "then B" get queued, so it comes last. The timeout waits for the next macrotask. The numbers in the labels are just names; run it and read the real order.

async/await is the same machinery

await x pauses the function and schedules the rest as a microtask once x settles. Code before the first await runs synchronously.

JS Ordering with async functions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that first: start appears before main: end: calling an async function runs it synchronously until its first await.

Error handling

Rejected promises skip .then handlers until a .catch (or a second argument to then) handles them. With async/await, use try/catch. A rejection nobody handles is reported as an unhandled rejection, and in Node 15+ it ends the process.

JS then/catch/finally and try/catch

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Interview points: .catch returns a promise, so the chain continues; finally takes no argument and passes the value or error through; and an await is required for try/catch to see the rejection.

Promise combinators

All four take an iterable of promises. Know what each one does when something fails.

Method Resolves when Rejects when
Promise.all all fulfill (array of values) the first one rejects
Promise.allSettled all settle (array of status objects) never
Promise.race the first settles, either way the first to settle is a rejection
Promise.any the first one fulfills all reject (AggregateError)
JS The four combinators

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A related question is "sequential versus parallel". await a(); await b(); runs one after the other. await Promise.all([a(), b()]) starts both at once, which is usually what you want when they are independent. A for loop with await inside is sequential on purpose; array.map(async ...) starts everything immediately.

Common follow-up questions

  • Is JavaScript single-threaded? The language runs your code on one thread. The host (browser or Node) does timers, network and file work elsewhere and queues the callbacks.
  • Does setTimeout(fn, 0) run immediately? No. It runs after the current code and all microtasks, and browsers clamp nested timers to at least a few milliseconds.
  • Can microtasks block rendering? Yes. A microtask that keeps queueing more microtasks starves timers and rendering forever.
  • Why does an async function always return a promise? The return value is wrapped, and a thrown error becomes a rejection.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which runs first after the synchronous code finishes?

    Microtasks (promise callbacks) are drained completely before the loop takes the next macrotask such as a timer.
  2. 2. What does Promise.all do if one promise rejects?

    Promise.all rejects as soon as one input rejects. Use Promise.allSettled if you need every outcome.
  3. 3. Which combinator ends with an AggregateError?

    Promise.any fulfills with the first success. If every input rejects, it rejects with an AggregateError listing all the reasons.
  4. 4. In an async function, when does the code before the first await run?

    An async function runs like a normal function until it hits its first await.

Summary

  • One thread runs code; the event loop drains all microtasks after each macrotask.
  • Promise callbacks and code after await are microtasks; timers and events are macrotasks.
  • Solve ordering puzzles by running sync code, then microtasks, then the next timer.
  • Use .catch or try/catch with await; never leave a rejection unhandled.
  • all fails fast, allSettled never fails, race takes the first outcome, any takes the first success.

Next: Arrays, Objects and Functional Patterns.