Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Event Loop

An event loop decides when a program runs ready callbacks, such as click handlers, timer callbacks, and code that continues after an asynchronous operation. This lets the program handle other work while it waits.

Also known as: цикл событий

What an event loop does

Suppose a page sends a request for search results. The browser doesn't need to leave JavaScript running until the server replies. The code starts the request and gives control back to the browser, which can handle input in the meantime. When the result is ready, the code that uses it can run. The event loop coordinates when ready work gets its turn; it isn't what performs the network request.

Tasks, microtasks, and the call stack

On a browser page's main thread, synchronous JavaScript runs on a call stack. A timer callback can't interrupt a function that's already running. Instead, the browser makes that callback available as a task when the timer becomes due. Running a script and dispatching user input are other examples of tasks. Browsers can use multiple task queues, so there's no single global first-in, first-out line for all events.

Promise reactions, such as .then() handlers, are microtasks, as are callbacks scheduled with queueMicrotask(). At the end of a task, the browser drains the microtask queue, including any new microtasks added while it drains. The browser may then have a rendering opportunity before it takes another task. It doesn't paint after every task.

Example: predict the output

console.log('start');
setTimeout(() => console.log('timer'), 0);
Promise.resolve().then(() => console.log('promise'));
console.log('end');

The browser logs start, end, promise, then timer. The two synchronous calls run within the script. Because the promise is already fulfilled, attaching .then() queues its handler as a microtask. The timer callback runs in a later task. A delay of 0 is not a promise that the callback will run immediately or at an exact time.

Why it matters in practice

A long synchronous calculation on the main thread can delay both input handlers and rendering. Moving the calculation into a .then() handler doesn't move it off that thread, and a chain that keeps scheduling more microtasks can delay input and rendering too. To let the browser respond between chunks, schedule those chunks as separate tasks. To run the computation off the main thread, consider a Web Worker.

Node.js uses an event loop to handle other requests while nonblocking I/O is pending. That doesn't make a CPU-heavy request handler harmless: other callbacks on the same thread must still wait. Node.js also has its own loop phases and a separate process.nextTick() queue, so browser scheduling rules alone won't answer a Node-specific ordering question.

How it comes up in interviews

Expect a snippet that mixes synchronous logs, a timer, and a promise or await. State which runtime you're assuming, trace the synchronous code first, and then explain when each callback or continuation gets queued. With await, the code before the first suspension runs synchronously; the continuation runs later as a microtask, even if the awaited promise is already fulfilled.