Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 15.7 — Interview Deep Dive: The Event Loop and Promises
JavaScript · Example 1 of 5

Puzzle 1: sync, microtasks, then tasks

then B is queued only when then A runs, so it lands after queueMicrotask, which was already waiting. The timer waits for every microtask.

Async interview questions test one model: synchronous code runs to the end, then every microtask (promise callbacks, await continuations, queueMicrotask) runs, then one task (a timer, an event) runs, and the cycle repeats; with that model you can predict any output order and write Promise.all and debounce from scratch.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Predict the output: setTimeout(() => console.log(1)); Promise.resolve().then(() => { console.log(2); setTimeout(() => console.log(3)); }); console.log(4);

02

Write promiseRace(items) that settles like the first item to settle.

03

Write throttle(fn, ms) that runs fn at most once every ms milliseconds (leading call only).

04

Write sleep(ms) and use it in an async function that prints a, waits 100 ms, then prints b.

Explain it without notes

01

Explain the event loop, tasks and microtasks in your own words.

02

In what order do setTimeout, Promise.then, queueMicrotask and synchronous logs run, and why?

03

Implement Promise.all and list the edge cases you handled.

04

What is the difference between debounce and throttle? When would you use each?

Puzzle 1: sync, microtasks, then tasks

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

sync start
sync end
then A
queueMicrotask
then B
timeout