Skip to main content
Albero Academy

The Event Loop & Advanced Async

How JavaScript stays responsive on one thread — the event loop, micro/macro tasks, and the Promise combinators for running work in parallel.

Chapter 14JavaScript17 min readBeginner friendly
  • #JavaScript
  • #Async
  • #Event Loop

Chapter 5 showed how to write async code; this chapter explains why it works. JavaScript runs on a single thread yet never blocks on slow work. The mechanism is the event loop — understanding it demystifies async and helps you run independent work in parallel with the Promise combinators.

Single-threaded

JavaScript executes one thing at a time on the call stack. If a slow operation blocked the stack, the whole page would freeze. Instead, slow work (timers, network, file I/O) is handed off to the environment, which notifies JS when it's done.

The event loop

The event loop continuously checks: is the call stack empty? If so, take the next finished callback from the queue and run it. This is how a single thread juggles thousands of pending operations without waiting on any of them:

order surprises everyone
console.log("1");
setTimeout(() => console.log("2"), 0); // deferred
Promise.resolve().then(() => console.log("3"));
console.log("4");
// Output: 1, 4, 3, 2
// Synchronous code first (1, 4), then microtasks (3), then timers (2).

Micro vs macro tasks

There are two queues. Microtasks (resolved Promises, await continuations) run before macrotasks (setTimeout, events). After each synchronous chunk, the loop drains all microtasks before the next macrotask — which is why the Promise logs before the timer above.

Promise combinators

When you have several independent async operations, don't await them one by one — combine them:

CombinatorResolves when…
Promise.allALL succeed (rejects if any fails)
Promise.allSettledALL finish (never rejects — reports each result)
Promise.raceThe FIRST settles (success or failure)
Promise.anyThe first SUCCESS (ignores failures)
run in parallel
// Fetch three things at once, wait for all
const [user, courses, stats] = await Promise.all([
fetch("/api/user").then(r => r.json()),
fetch("/api/courses").then(r => r.json()),
fetch("/api/stats").then(r => r.json()),
]);
// Tolerate partial failure
const results = await Promise.allSettled(tasks);
results.forEach(r => r.status === "fulfilled" ? use(r.value) : log(r.reason));

Parallel vs sequential

sequential (slow) vs parallel (fast)
// ✗ Sequential — total time = sum of all
for (const id of ids) { await fetchUser(id); }
// ✓ Parallel — total time = the slowest one
await Promise.all(ids.map(id => fetchUser(id)));

Practice

Predict the output order of a snippet mixing console.log, setTimeout(…, 0) and a resolved Promise, then run it. Rewrite a sequential for-loop of independent fetches into a single Promise.all and note the speed-up.

Key takeaways

  1. 01JavaScript is single-threaded; slow work is handed off and its callback queued.
  2. 02The event loop runs queued callbacks whenever the call stack is empty.
  3. 03Microtasks (Promises/await) run before macrotasks (setTimeout/events).
  4. 04Promise.all (all succeed), allSettled (all finish), race (first settles), any (first success).
  5. 05Await independent work in parallel with Promise.all — not one-by-one in a loop.