JavaScript theorytheory 0/50 · 0%
Async · easy

12. The event loop

One thread, a task queue and microtasks.

JavaScript runs on a single thread. Long synchronous work blocks everything, including rendering. Asynchronous callbacks are queued: promise callbacks go on the microtask queue, which drains before the next macrotask such as a `setTimeout`.

setTimeout(() => console.log("timeout"));
Promise.resolve().then(() => console.log("micro"));
console.log("sync");
// sync, micro, timeout

That ordering explains most "why did this log first?" confusion.

Check your understanding

  1. 1. Which queue drains first?

  2. 2. How many threads run your JS by default?

  3. 3. What blocks the UI?