Coordinate asynchronous work

Why a Cached Callback and Its Promise Follow-Up Run Before a Timer

Trace a browser cache notification, its promise follow-up, a nested microtask and a zero-delay timer in one runnable example.

Editorial illustration for Why a Promise Handler Runs Before a Nested Cache Microtask

A cached value is ready now, but you want its notification to run after the current script finishes. In a browser Window, queueMicrotask() can defer that notification. If the notification schedules a resolved promise handler, and that handler schedules one more microtask, all three callbacks run before the zero-delay timer in the example below. The important detail is that each callback schedules the next: the trace does not need to decide where a new microtask sits relative to a promise handler that was already waiting.

The browser timing rule: finish the script, drain microtasks, then consider the next task

Starting a script is a task in the browser event-loop description given by MDN. When that task exits, the browser processes microtasks until none remain. That includes microtasks scheduled during the drain. setTimeout() schedules a task, so its callback cannot interrupt this microtask chain, even with a delay of zero.

This rule is enough to answer the practical question without turning the example into a general tour of the event loop. Finish the direct script calls first. Then follow each callback as it creates the next piece of work. Consider the timer only after the microtasks have finished.

Trace a cache notification that schedules its promise follow-up

This is a self-contained browser Window example, not a Node.js scheduling claim. Paste it into a browser console or run it in a page script:

const cachedValue = "ready";

function readCached(onData) {
  queueMicrotask(() => {
    onData(cachedValue);

    Promise.resolve().then(() => {
      console.log("promise handler");
      queueMicrotask(() => console.log("nested microtask"));
    });
  });
}

console.log("sync: start");
readCached((value) => console.log(`cache callback: ${value}`));
setTimeout(() => console.log("timer"), 0);
console.log("sync: end");

The cache-hit function has a value to deliver, but it queues the delivery rather than calling onData immediately. Nothing in the script queues the promise handler separately. The cache callback creates its resolved promise and attaches the handler when the cache callback runs. The handler, in turn, queues the final microtask. Promise callbacks and queueMicrotask() callbacks use the microtask queue in this browser context.

Check the logs, including the nested microtask and timer

The predicted output is:

sync: start
sync: end
cache callback: ready
promise handler
nested microtask
timer

The two synchronous lines print while the script task is running. Once it finishes, the queued cache callback logs the cached value and schedules the promise handler. That handler logs and schedules the nested microtask. The nested callback logs, leaving this chain with no further microtasks to run. Only after the drain can the browser proceed to another task, including the timer callback.

The sequence does not demonstrate how a newly queued microtask is positioned relative to an already-waiting promise handler. There is no such competing handler here: the cache callback schedules the handler, and the handler schedules the final callback. The supplied evidence establishes that microtasks added during a drain run before the next task, but does not settle that different, competing-callback ordering question.

Rendering is not another log line to insert between these callbacks. The MDN microtask guide says the browser may update rendering after the microtask queue empties. It does not promise a paint at that point.

When to use queueMicrotask() for a deferred cache notification

Choose queueMicrotask() when a cache-hit path should notify its caller after current synchronous work but before a later task. MDN’s Window API example uses it in a cache-hit branch to update data and dispatch a load event asynchronously. The API has been available across browsers since July 2020; that statement does not identify individual browser versions.

Keep the deferred work bounded. A callback that continually queues another microtask can keep the drain going and delay later tasks. Also consider error reporting when choosing how to defer the notification: MDN notes that exceptions in promise-based microtasks are reported as rejected promises, whereas exceptions in queueMicrotask() callbacks are reported as standard exceptions.

Find a note

Search by topic, title, or keyword.