Skip to content
dreamcode
dreamcode
Map
Concurrency
Lesson 47 of 48
+15 XP on finish
JS EXPERTChapter 9 · JS Expert

Microtasks and the Event Loop

JavaScript is single-threaded but runs concurrently via the Event Loop. Promises queue jobs in the Microtask Queue, executing before rendering or the callback macrotask queue.

Worked example

How it reads

  • Microtasks (Promise then, queueMicrotask) run immediately after current script
  • Macrotasks (setTimeout, event callbacks) run in subsequent tick loops
Cloud tip: Never block the event loop with long CPU-bound synchronous loops, or UI rendering will freeze.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a Promise.resolve().then(...) right after the queueMicrotask line that logs 2.5. Predict where it lands, then check: the output should be exactly 1, 2, 3, 2.5, 4.

Press Run to check your work.