Skip to content
jshacks.dev
Async

Why an awaited value becomes a microtask

Why an awaited value becomes a microtask in JavaScript: await 1 takes one tick and awaiting a thenable takes more. Node.js 24.

1 min read
Summarize in AI

await always continues in a microtask, but how many ticks that takes depends on what you await. A plain value takes one, and a thenable takes extra steps.

JavaScript
const order = [];
const thenable = {
  then(resolve) {
    order.push('then called');
    resolve('t');
  },
};

(async () => {
  await 1;
  order.push('after await 1');
})();
(async () => {
  await thenable;
  order.push('after await thenable');
})();
Promise.resolve()
  .then(() => order.push('tick 1'))
  .then(() => order.push('tick 2'))
  .then(() => order.push('tick 3'));

await new Promise((resolve) => setTimeout(resolve, 0));
console.log(order.join(' > '));
Text
after await 1 > then called > tick 1 > after await thenable > tick 2 > tick 3

await 1 wraps the value in a resolved promise and queues the continuation at once. A thenable is different: the engine queues a job that calls its then method, and only after then has called resolve does the continuation get queued, which takes the extra tick. The tick chain shows the position: the thenable continuation lands between tick 1 and tick 2. The rules are in the ECMAScript spec for await.

Do not write code that depends on this order. It matters when you debug an interleaving between several async functions.

Taggednode 24
Share

More snippets

Async

Sleep for a number of milliseconds

Sleep for a number of milliseconds in JavaScript with a promise around setTimeout, or timers/promises in Node.js 24, and await it.

1 min read
Node.js

Read a text file with fs promises

Read a text file with fs promises in Node.js: await readFile with an encoding, and catch the error code when the file is missing. Node.js 24.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.