Skip to content

JavaScript runtime & language mechanics

How JavaScript runs your code: one thread, the event loop, promises and cancellation, plus how DOM events and memory work.

7 lessons

Async execution model

  1. 1Run-to-completion on one threadA running task is never set down for another: whatever arrives waits its turn, and a long task makes everything wait.
  2. 2Promise states: pending, fulfilled, rejectedA promise holds one result: the first resolve or reject wins, a thenable is followed, and reactions always run later.
  3. 3Promise combinators and concurrency limitsCombinators decide when you hear back, not what keeps running. Cancelling needs an AbortSignal, and a concurrency cap needs a pool.
  4. 4Cancellation and out-of-order responsesPromises cannot be cancelled, so you abort cooperatively through a signal, or ignore stale responses; either way the server keeps working.
  5. 5Debounce vs throttleDebounce waits for quiet and fires once; throttle fires on a steady beat while the noise continues. They answer different questions.

DOM events and memory

  1. 6Event propagation and delegationAn event goes down to the element and back up: stopping that trip and cancelling the browser's action are two different calls.
  2. 7Garbage collection and memory leaksGC frees the unreachable, not the unused: a leak is a forgotten reference path from a root.