Home Practice Event loop Practice · JavaScript
The event loop, step by step Pick a program and guess what it prints. Then step through it: the call stack, the timers, the microtask and task queues, and the console. Every program here prints in Node exactly what the steps show.
Program setTimeout 0 vs a promise The interview classic What await really does The promise executor runs now Microtasks run between tasks A microtask that adds a microtask Timers go by their delay queueMicrotask and then A timer of 0 ms still waits for the script and every microtask. Promises go first.
First, your guess: what will it print? (in order, spaces between)
1 console.log("A");2 setTimeout(() => console.log("B"), 0);3 Promise.resolve().then(() => console.log("C"));4 console.log("D");Timers (Web APIs)
none waiting
Step 1 of 14. The whole script runs first, as one task, on the call stack.
← Back Next step → Play Start again
The rule that answers every question Run the whole script, all the synchronous code, to the end. Run every microtask, including the ones added while doing so. Take one task (like a timer's callback), run it, then go back to step 2. Microtasks and tasks Microtasks : .then, .catch and .finally callbacks, the code after an await, and queueMicrotask.Tasks (also called macrotasks): setTimeout and setInterval callbacks, events, and I/O.Exam traps setTimeout(fn, 0) doesn't mean now: it waits for the script and for every microtask.The function you pass to new Promise runs straight away. Only .then waits. An async function runs synchronously until its first await. Learn it properly in Inside JavaScript: the event loop ,promises and async/await . Then test yourself with the code-reading drills .