JavaScript: the event loop as a cooperative scheduler

JavaScript runs your code on one thread. Instead of preempting a running task, the runtime uses cooperative scheduling: a task runs to completion, and only then does the event loop pick the next one. Long synchronous work therefore blocks everything else.

JavaScript
console.log("script start");

setTimeout(() => console.log("timeout (macrotask)"), 0);
Promise.resolve().then(() => console.log("promise (microtask)"));
process.nextTick(() => console.log("nextTick"));

console.log("script end");
Read the full lesson