Browser runtime, async JavaScript, and the event loop
- •
Execution model
- •
Synchronous vs asynchronous
- •
Event loop, tasks, and microtasks
- •
- •
- •
Microtasks
- •
sources: Promise reactions,
queueMicrotask(), MutationObserver delivery. - •
timing: at a checkpoint after current JS unwinds, before the next task.
- •
returning from one function is not enough while its caller still executes.
- •
Execution -> callbacks run synchronously on the stack when their turn arrives.
- •
continually adding microtasks can starve tasks/rendering; keep work short.
- •
- •
- •
Ordering example
- •
console.log("A"); setTimeout(() => console.log("task"), 0); Promise.resolve().then(() => console.log("microtask")); console.log("C"); // A -> C -> microtask -> task
- •
- •
Debounce
- •