How the event loop runs your code
JavaScript runs one thing at a time, on one call stack. The script itself is the first task. When it finishes, the event loop runs every microtask (promise callbacks, code after await, queueMicrotask), and only then takes the next task, such as a setTimeout callback or a network response. After each task, the microtasks run again.
That's why a promise callback runs before a timer with a delay of 0: the timer's callback has to wait for a task turn, while the promise callback is a microtask.
How to use the JavaScript Event Loop Visualizer
- Write or paste JavaScript in the editor, or load an example.
- Press Visualize (or Ctrl+Enter, ⌘+Enter on a Mac).
- Step with the buttons, the slider or the arrow keys, or press Play.
- Read what each step did in the step-by-step list, and watch the queues change.
- Press Edit code to change the code and visualize it again.
Examples
Microtasks run before timers
console.log("1: script start");
setTimeout(function timer() {
console.log("5: timeout");
}, 0);
Promise.resolve().then(function onResolved() {
console.log("3: then");
});
queueMicrotask(function micro() {
console.log("4: microtask");
});
console.log("2: script end");1: script start
2: script end
3: then
4: microtask
5: timeoutThe then callback and the queueMicrotask callback are microtasks: they run as soon as the script ends. The timer waits for the next task, even with a delay of 0.
Where an async function pauses
async function main() {
console.log("2: main starts");
await helper();
console.log("4: main resumes");
}
async function helper() {
console.log("3: helper runs");
}
console.log("1: before");
main();
console.log("3b: after main() returned a promise");1: before
2: main starts
3: helper runs
3b: after main() returned a promise
4: main resumesAn async function runs synchronously until its first await. The rest of it resumes in a microtask, after the script has finished.
Returning a promise from then costs two microtasks
Promise.resolve()
.then(function first() {
console.log("a: returns a promise");
return Promise.resolve("a: done");
})
.then(function second(value) {
console.log(value);
});
Promise.resolve()
.then(() => console.log("b: 1"))
.then(() => console.log("b: 2"))
.then(() => console.log("b: 3"));a: returns a promise
b: 1
b: 2
b: 3
a: doneThe promise returned by the first callback has to be adopted by the next promise in the chain, which takes two extra microtasks. Chain b runs its three callbacks in the meantime.
setTimeout in a loop, with var and let
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log("var i:", i), 0);
}
for (let j = 0; j < 3; j++) {
setTimeout(() => console.log("let j:", j), 0);
}var i: 3
var i: 3
var i: 3
let j: 0
let j: 1
let j: 2Every callback runs after both loops have finished. There's one var i, which is 3 by then; let gives each iteration its own j.
What the panels show
Call stack: the functions running now, the most recent on top. Web APIs: timers and requests the browser is waiting on, with the simulated time they're due. Task queue and microtask queue: callbacks that are ready, in the order they'll run. The step-by-step list says what each step did, and the highlighted code shows the statement that runs next.
Time is simulated: when nothing is left to run, the clock jumps to the next timer, so a 5-second timeout takes no real time to visualize.
Why you can trust the order
The visualizer runs your code on its own event loop, with a Promise implementation that follows the specification step by step and passes the official test262 Promise tests. After each run it also runs the same code for real in your browser and compares the console output. If they differ, you see a warning instead of a wrong animation.
Options
- fetch delay
- How long the simulated
fetchtakes to respond, in simulated time. Every request gets a JSON body with its URL. - Speed
- How fast Play moves through the steps: from half speed to four steps a second.
Limitations
- Supported: functions,
setTimeout,setInterval,clearTimeout,clearInterval, promises and their static methods,async/await,queueMicrotask,consoleand a simulatedfetch. - Not supported: DOM events,
requestAnimationFrame,MessageChannel, Node.js APIs such asprocess.nextTick, generators andfor await. - Code can be up to 500 lines (20,000 characters): the visualizer is for snippets. Longer code runs in the JavaScript Playground.
- A recording stops after 10,000 steps, 5 seconds of running or one hour of simulated time.
fetchis simulated: no request leaves your browser, and every response is a small JSON body.
Frequently asked questions
Why does a promise callback run before setTimeout with 0 ms?
Promise callbacks are microtasks, and the event loop empties the microtask queue as soon as the call stack is empty. A timer's callback is a task: it waits until the current task and all its microtasks have finished.
Is my code sent anywhere?
No. It runs in your browser, in the same sandbox as the JavaScript Playground: an isolated frame and a Web Worker with no network access. Nothing you type is uploaded or sent to analytics.
Why does returning a promise from then take longer?
When a then callback returns a promise, the outer promise has to adopt its state. The specification does that with an extra job that calls the inner promise's then, which adds two microtask turns. Load the example and step through it to see each one.
Can I share code with someone?
Yes. Copy share link puts your code in the link itself, after the #, which browsers never send to a server. Whoever opens it gets the code in the editor and presses Visualize to see it run.
What if the visualizer is wrong about my code?
After each run, the same code also runs for real in your browser, and the console output is compared. If they differ you'll see a warning with the first line that differs. Code that depends on the current time or random numbers can differ for that reason alone.