Hunt
Skip to content

JavaScript Event Loop Visualizer

Paste JavaScript with promises, async/await, timers or fetch, press Visualize, and step through it: the line that runs, the call stack, the timers waiting in the Web APIs, the task queue and the microtask queue. Every step is explained. The order follows the JavaScript and HTML specifications, and is checked against a real run of the same code in your browser.

Runs in your browser

Sync code, microtasks, then timers

The script is a task, so it runs to the end first. Then every microtask runs: the then callback and the queueMicrotask callback. The timer's callback is a task of its own, so it waits for the next turn of the event loop, even with a delay of 0.

Code

Event loop

Simulated time: 0 ms

Not started

Press Visualize to record the steps.

Call stack

Empty

Web APIs

No timers or requests

Microtask queue

Empty

Task queue

Empty

Console

Nothing printed yet.

    Step by step

    Press Visualize to record the steps.

    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

    1. Write or paste JavaScript in the editor, or load an example.
    2. Press Visualize (or Ctrl+Enter, ⌘+Enter on a Mac).
    3. Step with the buttons, the slider or the arrow keys, or press Play.
    4. Read what each step did in the step-by-step list, and watch the queues change.
    5. Press Edit code to change the code and visualize it again.

    Examples

    Microtasks run before timers

    Input
    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");
    Output
    1: script start
    2: script end
    3: then
    4: microtask
    5: timeout

    The 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

    Input
    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");
    Output
    1: before
    2: main starts
    3: helper runs
    3b: after main() returned a promise
    4: main resumes

    An 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

    Input
    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"));
    Output
    a: returns a promise
    b: 1
    b: 2
    b: 3
    a: done

    The 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

    Input
    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);
    }
    Output
    var i: 3
    var i: 3
    var i: 3
    let j: 0
    let j: 1
    let j: 2

    Every 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 fetch takes 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, console and a simulated fetch.
    • Not supported: DOM events, requestAnimationFrame, MessageChannel, Node.js APIs such as process.nextTick, generators and for 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.
    • fetch is 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.