arthrop0d

Brief articles on varied topics.

JavaScript Browser Tasks and Microtasks

javascriptweb development

JavaScript browser tasks are units of work the browser runs one at a time; microtasks are smaller callbacks the browser drains at specific checkpoints, usually after each task. Together, they let browsers handle events, timers, network results, and promise continuations without running JavaScript simultaneously on the same page.

The problem this solves

A browser has more work to do than execute your JavaScript. It must receive input, process timers and network results, calculate layout, paint pixels, and respond to other pages and browser features. JavaScript, meanwhile, is generally run to completion: once a callback starts, another JavaScript callback cannot interrupt it halfway through.

Without a scheduling mechanism, a long-running script could block input and visual updates indefinitely. If every callback ran immediately when its trigger occurred, callbacks could also interleave unpredictably and corrupt assumptions about shared state.

The event loop provides an ordering rule. Work is placed into queues, JavaScript runs one callback at a time, and the browser gets defined points at which to process smaller follow-up jobs and consider rendering.

The event loop does not make JavaScript parallel. It decides which ready piece of work runs next.

Tasks: the browser's larger units of work

A task is a callback-sized unit of work scheduled by the browser. Common examples include:

People often call these macrotasks, especially when contrasting them with microtasks. “Task” is the terminology used by the browser platform; “macrotask” is common shorthand.

A task runs from start to finish. Consider this example:

console.log("task start");

setTimeout(() => console.log("timer task"), 0);

Promise.resolve().then(() => console.log("promise microtask"));

console.log("task end");

The initial script is itself a task, so it prints task start and task end first. The timer does not run immediately: 0 means “no earlier than the timer's eligibility,” not “interrupt this script.” The promise callback is a microtask, and it runs after the current task finishes. The usual output is:

task start
task end
promise microtask
timer task

The exact choice among unrelated task sources can involve browser scheduling rules, but a timer cannot jump into the middle of the current task.

Microtasks: follow-up work before the browser moves on

A microtask is a callback intended to run as soon as the current JavaScript stack is empty and the browser reaches a microtask checkpoint—the point where it drains the microtask queue. Promise reaction callbacks, such as those created by .then, .catch, or .finally, are microtasks. So is a callback queued with queueMicrotask(). MutationObserver callbacks also use this mechanism.

After a task completes, the browser repeatedly takes the oldest microtask and runs it. If that callback queues another microtask, the new callback is added to the same queue and also runs before the checkpoint ends:

queueMicrotask(() => {
  console.log("first");
  queueMicrotask(() => console.log("second"));
});

console.log("task");

The output is task, first, second. The browser does not render or start an unrelated task between first and second merely because the first microtask finished.

This ordering is useful for preserving consistency. A promise can settle during one operation, and its handlers can observe the completed state before unrelated browser work runs. It also explains familiar behavior in asynchronous code: resolving a promise does not call its handler inline, but the handler generally runs before a timer or user event that becomes eligible afterward.

The event-loop sequence

A simplified browser cycle looks like this:

  1. Choose a runnable task.
  2. Run that task to completion.
  3. Perform a microtask checkpoint, draining all currently queued microtasks and any they create.
  4. At an appropriate point, perform rendering work if the browser decides an update is needed.
  5. Choose another task and repeat.

This is a teaching model rather than a promise that every browser performs exactly one task per visual frame. Browsers have multiple task sources and scheduling priorities, and the HTML standard gives the user agent room to choose work. The important guarantees are the ordering within a callback and the microtask checkpoint behavior.

A task that takes 200 milliseconds blocks everything behind it: input handling, other tasks, and rendering. A short task can also cause a long delay if it continually generates microtasks:

function keepGoing() {
  queueMicrotask(keepGoing);
}

keepGoing();

Because the microtask checkpoint must keep draining, the browser may never reach another task or a rendering opportunity. This is microtask starvation. The same practical lesson applies to a large finite batch of microtasks: promises do not automatically yield to painting.

When can the browser render?

Rendering is not a callback that runs after every line or every promise. The browser may consider updating styles, layout, and pixels after a task and its microtask checkpoint, before it begins more work. This is a rendering opportunity. It may skip that opportunity when nothing visual changed, the page is not visible, or scheduling and performance considerations make an update unnecessary.

So this sequence does not necessarily show an intermediate state on screen:

element.textContent = "Loading...";

Promise.resolve().then(() => {
  element.textContent = "Ready";
});

The text can be changed to Loading... during the task, then changed to Ready during the microtask checkpoint, before the browser paints either state. To reliably give the browser a chance to display the first state, yield to a later task or use a rendering-oriented API such as requestAnimationFrame for work intended to occur before the next paint.

requestAnimationFrame callbacks belong to the browser's rendering cycle: they are invoked before a frame is painted when the document is eligible for animation updates. They are not a general replacement for timers, and a callback still needs to finish quickly if the frame is to meet its deadline.

A practical debugging rule

When an incident report says “the promise callback ran before the UI updated,” that is usually the expected ordering, not a broken promise implementation. Ask three questions:

Keeping those distinctions clear explains many timer-versus-promise surprises, delayed input, and interfaces that update only after a large asynchronous chain finally yields.

← All articles