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
Run the script from top to bottom. It is the first task.
- 2
When the call stack is empty, run every microtask, including any that are added while they run.
- 3
Take the oldest task from the task queue and run it to the end.
- 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.
- setTimeout vs a promise Synchronous code runs first, then every microtask, then the next task. A 0 ms timer still waits for all of them.
- async and await An async function runs synchronously until its first await. The rest of it waits in the microtask queue while the caller carries on.
- Microtasks jump the queue Microtasks added while microtasks are running still run before any task, so a chain of promises finishes before a timer.
- Timers that schedule promises After each task, the event loop empties the microtask queue. A promise created inside a timer runs before the next timer.
- var vs let in a loop Each timer callback runs after the loop has finished. With var they share one i; with let each iteration gets its own.
- Different timer delays Timers wait in the browser, not on the call stack. The shortest delay reaches the task queue first, whatever order they were set in.
- new Promise runs straight away The function passed to new Promise runs synchronously. Only the callbacks registered with then wait for the microtask queue.
- fetch and async/await fetch hands the request to the browser and returns a pending promise. The response comes back as a task, and await resumes in a microtask.
- Errors and catch A rejected promise skips then callbacks until a catch. An await on a rejected promise throws inside the async function.
- Returning a promise costs ticks Resolving a promise with another promise takes two extra microtasks, so an async function that returns a promise finishes later.
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.
- Predict the event-loop order of
setTimeout, promises,queueMicrotaskandawaiteasy · 15 min · Free - Predict nested event loop order: promises that schedule timers, timers that schedule promises, and
awaitin loopsmedium · 25 min · Pro - Predict async function timing:
returnversusreturn await, thenables, and extra microtask ticksmedium · 30 min · Pro - Build a Zone.js-style task tracker: pending macrotasks and microtasks,
isStableandwhenStable()hard · 55 min · Pro
Short answers to read first
- What is the event loop in JavaScript?
- What happens when microtasks keep scheduling more microtasks?
- Async/await vs promises: what is the difference?
- What is a Promise in JavaScript?
- setTimeout vs setImmediate vs process.nextTick
- Synchronous vs asynchronous JavaScript: what is the difference?
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.