JavaScript: Promise and event loop (2026)

By Xah Lee. Date: .
work in progress

wow amazing. javascript promise is not async. it run task 'in-parallel' only when whatever function u call inside it is already async.

completely throws off my understanding.

what shit.

js promise 2026-11-24 36a46 L
js promise 2026-11-24 36a46 L

Javascript promise is not about making a big function to run concurrently (async).

But rather, represent and compose operations that are already async.

what extreme idiocy.

js promise 2026-11-24 3672d L
js promise 2026-11-24 3672d L
work in progress

in js event loop, we have the following steps. but when is each js construct added to macro or micro task?
The loop’s algorithm
Run the current synchronous code until the call stack is empty.
Drain the entire microtask queue (including any microtasks that those microtasks themselves enqueue).
(Browser) Optionally update rendering / paint.
Dequeue and run one macrotask.
Go back to step 2.

(ai answer)

Macrotasks (tasks) and microtasks are enqueued at the moment the relevant API is called (or when an external event/timer/promise settles). The event loop then processes them according to the algorithm you listed.

Microtasks

Enqueued into the microtask queue (higher priority; fully drained after the current task / before the next macrotask or render):

Construct When it is added
Promise.then / .catch / .finally When the promise settles (or immediately if already settled)
Continuation after await Same as the underlying promise reaction
queueMicrotask(fn) Immediately when called
MutationObserver callback When a DOM mutation is observed (queued as a microtask)
process.nextTick (Node) Immediately; runs before other microtasks

Macrotasks (tasks)

Enqueued into a task queue (one is taken per loop iteration, after microtasks are drained):

Construct When it is added
setTimeout / setInterval callback When the timer expires (the timer itself is started by the host when you call the function)
DOM events (click, input, etc.) When the browser dispatches the event
Network / I/O callbacks (XHR, most fetch response handling, Node I/O) When the I/O operation completes
MessageChannel / postMessage When the message is received
Initial script execution The script itself is the first macrotask
setImmediate (Node) In the “check” phase of Node’s event loop

Notes on timing and special cases

Classic order demonstration:

console.log('sync');
setTimeout(() => console.log('macrotask'), 0);
Promise.resolve().then(() => console.log('microtask'));
console.log('sync end');
// sync → sync end → microtask → macrotask

This matches the loop you described: finish current task → drain all microtasks → (optional render) → take one macrotask → repeat.

but if i have const xpromise = new Promise(fExecutor); xpromise.then(fOnFulfilled, fOnRejected); the executor may take a minute, since new Promise is not enqueued, so the js engine waits for it to finish?

Yes — the executor runs synchronously.

const xpromise = new Promise(fExecutor);  // fExecutor is called right now, on the current stack
xpromise.then(fOnFulfilled, fOnRejected);

In normal usage the executor returns quickly: it starts some async work (timer, network, etc.) and calls resolve/reject later. Only then does the promise settle, and only then are fOnFulfilled / fOnRejected scheduled as microtasks.

So the engine “waits” only if the executor itself is blocking. The Promise machinery does not make the executor asynchronous.

wow. but i thought promise is specifically designed to make long-time tasks to seems to run in parallel. i dont understand. lets say i have a long time function named longf. i call inside promise, it still runs long time because js wait for it to finish.

Promises do not run CPU work in parallel or make a long synchronous function non-blocking.

JavaScript is single-threaded. The Promise constructor just runs its executor right away on the main thread:

function longf() {
  // heavy loop, big calculation, etc.
  for (let i = 0; i < 1e10; i++) {}
}

new Promise((resolve) => {
  longf();          // still blocks everything until it finishes
  resolve();
});

While longf is running, the event loop is frozen — no clicks, no timers, no other promises, no rendering.

Promises are only useful for already-asynchronous work that the browser/Node does off the main thread (network, timers, file I/O, etc.). When that work finishes, it notifies JavaScript by settling the promise, and the .then runs later as a microtask.

To keep the UI responsive for long CPU work you must either:

Wrapping a synchronous function in a Promise changes nothing about when or where it runs.

so what is the point of JavaScript promise object since any big task you need to do must already be a sync function you put inside promise otherwise it still waits.

The point of a Promise is not to turn a long synchronous function into something non-blocking.

It is a standardized way to represent and compose operations that are already asynchronous — work that the browser or Node does outside the JavaScript thread and then notifies you about later.

Typical pattern (executor returns immediately):

function delay(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);   // schedules a macrotask, then returns
  });
}

fetch('/api/data')             // network request starts in the browser
  .then(r => r.json())         // .then is a microtask
  .then(data => process(data));

If the work is pure CPU-bound and synchronous, a Promise does nothing useful for you. You either leave it on the main thread (and accept the freeze) or move it to a Web Worker. Promises solve the coordination problem for the async cases that already exist.