Synchronous code runs first, then every microtask, then the next task. A 0 ms timer still waits for all of them.

console.log("A");

setTimeout(() => {
  console.log("B");
}, 0);

Promise.resolve().then(() => {
  console.log("C");
});

console.log("D");

Call stack

Web APIs

Microtask queue

Task queue

See exactly when your JavaScript runs.

Paste any snippet and step through it. Watch each function go on the call stack, timers wait in the browser, and callbacks line up in the task queue and the microtask queue, with every step explained in plain English.

Your own code, step by step

Paste a snippet or pick an example, then step forward and back or press Play. Drag the slider to jump to any moment.

All four parts at once

The call stack, Web APIs, task queue and microtask queue sit side by side, and each callback moves between them as it would in a browser.

The order a browser uses

Promises, await, finally and thenables follow the JavaScript spec, extra microtask ticks included. Our tests check the printed order against a real JavaScript engine.

Every step explained

Each step comes with one sentence on what just happened and why, like why a 0 ms timer still runs after a promise.

Share with a link

Share copies a link with your code inside the URL itself. Nothing is uploaded, and anyone who opens it sees the same code.

Safe by design

Your code is never run as real JavaScript on this page. A small interpreter steps through it instead, so a shared link can't touch this site or your accounts.

How the JavaScript event loop works

JavaScript runs on a single thread. Four parts decide what runs, and when. The visualizer shows each of them as a panel.

Call stack

Where JavaScript runs, one thing at a time. Calling a function puts it on top. Returning takes it off. While anything is on the stack, nothing else can run.

Web APIs

Work the browser does for you outside JavaScript: timers from setTimeout and setInterval, network requests from fetch, and events. When the work is done, a callback is queued.

Task queue

Callbacks from timers, network responses and events wait here. The event loop takes one task at a time, and only when the call stack is empty.

Microtask queue

Promise callbacks (then, catch, finally), the rest of an async function after await, and queueMicrotask. It is emptied completely after every task, before the next task starts.

The loop, in order

  1. 1

    Run the script from top to bottom. It is the first task.

  2. 2

    When the call stack is empty, run every microtask, including any that are added while they run.

  3. 3

    Take the oldest task from the task queue and run it to the end.

  4. 4

    Run every microtask again, then take the next task. When nothing is left, wait for a timer or a response.

Examples to step through

Each one shows one rule. Open it in the visualizer above, or pick it from the example menu.

What it can run

A subset of JavaScript that covers almost every event loop question.

  • Functions, arrow functions, closures, objects, arrays and destructuring
  • if, for, for…of, while, switch, try/catch/finally and throw
  • Promises: new Promise, then, catch, finally, Promise.resolve, reject, all, allSettled, race and any
  • async functions and await
  • setTimeout, setInterval, their clear functions, and queueMicrotask
  • fetch, simulated: it answers after about 200 ms with a sample response
  • console.log, info, warn and error

Classes, generators, regular expressions and the DOM aren't supported yet. It models the browser, so Node-only functions such as process.nextTick and setImmediate aren't available.

Practise event loop interview questions

Predict-the-output questions are a favourite in frontend interviews. Work out the order, then check it here.

Short answers to read first

Read the full guide: JavaScript event loop explained: tasks, microtasks and rendering

Questions about the event loop

What is the event loop in JavaScript?

JavaScript runs on one thread, so it can do one thing at a time. The event loop decides what runs next: it runs a task such as your script or a timer callback, then every waiting microtask such as promise callbacks, then the next task, and so on. Timers and network requests wait in the browser meanwhile, so long waits never block the page.

What is the difference between the task queue and the microtask queue?

Tasks are callbacks from timers, network responses and events. Microtasks are promise callbacks, the code after await and queueMicrotask callbacks. After each task, the event loop runs every microtask before it takes the next task, so microtasks always go first.

Why does setTimeout with 0 ms run after a promise?

setTimeout puts its callback in the task queue, while a promise's then callback goes into the microtask queue. When the script finishes, the event loop empties the microtask queue first, so the promise callback runs before the timer, even with a 0 ms delay.

Does await block the thread?

No. await pauses only the async function it is in. The function leaves the call stack, the code that called it carries on, and the rest of the function runs later as a microtask once the awaited promise settles.

Is this the same as the Node.js event loop?

This visualizer models the browser. For code that uses only promises, async/await and setTimeout, Node.js (version 11 and later) prints the same order. Node also has process.nextTick, setImmediate and separate phases, which aren't shown here.

Is my code sent to a server?

No. Everything happens in your browser. Share links keep the code in the part of the URL after #, which browsers never send to a server.

Can I use it in a blog post, course or class?

Yes, it is free. Pick an example or paste your own code, press Share and use the link. It opens this page with the same code, ready to step through.

Also freeRun the code for real in the JavaScript editor