JavaScript Developer Free Lesson: The Event Loop

Free sample lesson from the JavaScript Developer Cert Lab. The full course has 25 lessons and 4 full practice exams.

Unlock every section for $9.99
Asynchronous Programming · AP3 · part of the 13% section · about 20 min with practice

AP3: The event loop, tasks and microtasks

You’ll be able to: predict the order of logs from sync code, promise callbacks and timers.

Run order

KindExamplesWhen it runs
Synchronous codeconsole.log, loopsNow, to completion
Microtasksthen callbacks, await continuations, queueMicrotaskRight after the current code, before the next task
TaskssetTimeout, setInterval, eventsAfter the microtask queue is empty

Run to completion

JavaScript runs one job at a time. When the stack is empty, every queued microtask runs, then the next task. A setTimeout with 0 ms still waits for all of that.

order.js
1console.log('A');
2setTimeout(() => console.log('B'), 0);
3Promise.resolve().then(() => console.log('C'));
4queueMicrotask(() => console.log('D'));
5console.log('E');
6// A, E, C, D, B

Exam trap

A long synchronous loop blocks timers, clicks and rendering. Nothing else runs until it finishes.

Predict, then reveal

Which logs first: a resolved promise’s then or setTimeout 0?

Show the answer

The then callback, because it’s a microtask.

Try it 5 min · your browser console or Node.js

  1. Paste order.js into the console and check the order.
  2. Add a while loop that runs for 2 seconds before the timeout and watch it delay B.
  3. Use queueMicrotask inside a then callback and log the order.

Practice questions

Ten exam-style questions on this lesson. Answer each one before you open it.

Question 1. console.log(1); setTimeout(() => console.log(2), 0); Promise.resolve().then(() => console.log(3)); console.log(4); What’s the order?

  • A. 1, 2, 3, 4
  • B. 1, 4, 3, 2
  • C. 1, 4, 2, 3
  • D. 1, 3, 4, 2, because a resolved then runs at once
Show the answer and explanation

Answer: B. Sync first, then microtasks, then the timer task.

setTimeout 0 still waits for microtasks.

Question 2. Which callbacks run as microtasks? (Choose 2.)

  • A. Promise then callbacks
  • B. queueMicrotask callbacks
  • C. setTimeout callbacks
  • D. click handlers that the user triggered earlier
Show the answer and explanation

Answer: A, B. Promise reactions and queueMicrotask use the microtask queue.

Timers and events are tasks.

Question 3. A 3-second synchronous loop runs right after setTimeout(fn, 0). When does fn run?

  • A. After 0 ms, interrupting the loop
  • B. Never
  • C. In parallel, on another thread
  • D. After the loop finishes
Show the answer and explanation

Answer: D. Run-to-completion means the loop finishes first.

JavaScript doesn’t interrupt running code.

Question 4. What does run-to-completion mean?

  • A. Every promise must fulfill
  • B. Async functions finish before the page can paint again
  • C. Each job finishes before another starts
  • D. Timers always run on time
Show the answer and explanation

Answer: C. Jobs don’t interrupt each other.

Timers can run late.

Question 5. setTimeout(fn, 1000) is called. What does the delay guarantee?

  • A. fn runs no sooner than about 1 second
  • B. fn runs before any promise callback queued after it
  • C. fn runs within 1 second
  • D. fn runs exactly at 1 second
Show the answer and explanation

Answer: A. The delay is a minimum; busy code can push it later.

It isn’t exact.

Question 6. async function f() { console.log(‘a’); await null; console.log(‘b’); } f(); console.log(‘c’); What’s the order?

  • A. a, b, c
  • B. c, a, b
  • C. a, b, c, since await null doesn’t pause
  • D. a, c, b
Show the answer and explanation

Answer: D. Code before await runs now; the rest is a microtask.

c is sync and runs before the continuation.

Question 7. Which statements about the event loop are true? (Choose 2.)

  • A. Promise callbacks run in a new thread
  • B. setTimeout 0 runs before microtasks
  • C. Microtasks run before the next task
  • D. A long sync loop blocks rendering
Show the answer and explanation

Answer: C, D. The microtask queue empties first, and sync code blocks everything.

Timers wait; there’s no new thread.

Question 8. A click handler’s promise resolves, and a 0 ms timer was also queued earlier. What runs next after the handler finishes?

  • A. The timer
  • B. The promise’s then callback
  • C. The next click, if the user clicked again
  • D. A repaint, always
Show the answer and explanation

Answer: B. Microtasks run before the next task.

The timer is a task.

Question 9. Which API queues a microtask directly?

  • A. setInterval(fn, 0) with a cleared id
  • B. requestAnimationFrame()
  • C. queueMicrotask()
  • D. setTimeout(fn, 0)
Show the answer and explanation

Answer: C. queueMicrotask adds to the microtask queue.

The others schedule tasks or frames.

Question 10. Promise.resolve().then(() => console.log(‘x’)); console.log(‘y’); What’s logged first?

  • A. y
  • B. Both at once
  • C. x
  • D. Neither; the then callback needs await
Show the answer and explanation

Answer: A. then callbacks run after the current sync code.

Even a resolved promise’s callback is async.

Before you move on

1. Microtasks or tasks first?

Show answer

Microtasks.

2. Does setTimeout 0 run immediately?

Show answer

No, after sync code and microtasks.

Key takeaways

  • Sync code runs to completion first.
  • Microtasks run before the next task.
  • Timer delays are minimums.
  • Long sync work blocks everything.

Sources

Checked against MDN Web Docs, Node.js, npm, Jest, Chrome DevTools docs and Trailhead on Oct 23, 2026. Questions follow the current JavaScript Developer exam guide.

Liked this lesson?

There are 24 more like it, plus section checks, flashcards and 4 full 60-question practice exams.

Start studying for $9.99