Skip to content
jshacks.dev
Async

Thenables: Objects That Act Like Promises

Learn what a thenable is in JavaScript, how await and Promise.resolve adopt it, the extra microtask it costs and the traps of objects with a then method.

6 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, npm and Node.js logos.

A thenable is any object with a then method. It does not have to be a promise, or inherit from one, and that loose definition is the reason promises from different libraries work together. await, Promise.resolve, Promise.all and returning a value from a then handler all recognize thenables and follow them, calling their then with a pair of callbacks and waiting for one of them to be called. The design is a compatibility layer from the era of competing promise libraries, and it is still in the language. This post explains how adoption works on Node.js 24 with printed output, shows how to write a thenable on purpose, covers the cost of assimilation, and lists the traps: an object that is accidentally a thenable, a then that calls back twice and a thenable that throws.

What makes something a thenable

The Promises/A+ specification, which the JavaScript promise follows, says that anything with a callable then property is a thenable. The engine does not check the type, the prototype or any other property.

JavaScript
const thenable = {
  then(onFulfilled, onRejected) {
    onFulfilled("hello from a thenable");
  },
};

console.log(thenable instanceof Promise);
console.log(await thenable);
Text
false
hello from a thenable

The object is not a promise, and await used it anyway. When await meets a value, it calls Promise.resolve on it, and Promise.resolve treats an object with a then function as a promise to follow.

How adoption works

When a thenable is resolved into a native promise, the engine does three things. It reads the then property once. It queues a microtask that calls then with two fresh functions, one that resolves the native promise and one that rejects it. And it waits for one of them to be called. This is why adoption is asynchronous even if the thenable calls back immediately.

JavaScript
const thenable = {
  then(resolve) {
    console.log("then called");
    resolve("value");
  },
};

console.log("before Promise.resolve");
const promise = Promise.resolve(thenable);
console.log("after Promise.resolve");
await promise;
console.log("settled");
Text
before Promise.resolve
after Promise.resolve
then called
settled

then was not called during Promise.resolve. It was called in a microtask afterwards. A native promise, by contrast, is returned by Promise.resolve unchanged, with no extra step. That extra microtask is the cost of generality: awaiting a thenable takes more ticks than awaiting a native promise.

Writing a thenable on purpose

The simplest use is a lazy value. Because the engine calls then only when someone awaits it, the work can be deferred until needed.

JavaScript
class LazyValue {
  #compute;

  constructor(compute) {
    this.#compute = compute;
  }

  then(resolve, reject) {
    return Promise.resolve()
      .then(() => this.#compute())
      .then(resolve, reject);
  }
}

const expensive = new LazyValue(() => {
  console.log("computing...");
  return 42;
});

console.log("created, nothing computed yet");
console.log("result:", await expensive);
Text
created, nothing computed yet
computing...
result: 42

The computation ran at the await, not at construction. Query builders use the same trick: a builder object is configured with chained calls and executes when awaited. Libraries such as database toolkits and some HTTP clients return thenables for this reason, so await db.select().where(...) works without a final .execute() call.

The same builders show the downside. A thenable runs its work each time it is awaited, and nothing caches the result, unlike a promise that settles once.

JavaScript
let runs = 0;

const twice = {
  then(resolve) {
    runs += 1;
    resolve(runs);
  },
};

console.log(await twice, await twice);
Text
1 2

Two awaits ran the work twice. If you want once-only behavior, convert it with Promise.resolve(thenable) and keep the promise.

Thenables in chains

Returning a thenable from a then handler makes the derived promise follow it, the same as returning a promise.

JavaScript
const result = await Promise.resolve(1).then((n) => ({
  then(resolve) {
    resolve(n + 100);
  },
}));

console.log(result);
Text
101

This is what lets old libraries return their own promise-like objects from a chain without breaking it. It is also how a native promise swallows a foreign one: after the first handler, the chain is native again.

Trap 1: an object that is accidentally a thenable

Any object with a then method that is not meant to be one gets adopted. A classic case is a module namespace or a mock with a then property, which makes await or Promise.resolve treat the object as a promise.

JavaScript
const accidental = {
  name: "report",
  then() {
    // this is a method named "then" for some unrelated reason
  },
};

const result = await Promise.race([
  Promise.resolve(accidental),
  new Promise((resolve) => setTimeout(() => resolve("gave up waiting"), 30)),
]);

console.log(result);
Text
gave up waiting

The accidental thenable never called its callbacks, so Promise.resolve never settled, and the race timed out. This bug is hard to diagnose because the object looks ordinary. The rule is to avoid naming a method or property then on objects that might be returned from an async function or passed to await. An async function that returns such an object hangs forever for the same reason.

Trap 2: calling back more than once

A thenable that calls resolve or reject several times is guarded by the adopting promise, which uses only the first call, and ignores the rest.

JavaScript
const noisy = {
  then(resolve, reject) {
    resolve("first");
    resolve("second");
    reject(new Error("ignored"));
  },
};

console.log(await noisy);
Text
first

The specification demands this protection, so a misbehaving thenable cannot confuse a native promise. It is still worth writing thenables that call back exactly once.

Trap 3: a then that throws

If then throws before calling back, the native promise rejects with the error. If it throws after calling back, the error is ignored.

JavaScript
const throwsFirst = {
  then() {
    throw new Error("broke before calling back");
  },
};

const throwsAfter = {
  then(resolve) {
    resolve("already settled");
    throw new Error("too late to matter");
  },
};

try {
  await throwsFirst;
} catch (error) {
  console.log("rejected:", error.message);
}

console.log(await throwsAfter);
Text
rejected: broke before calling back
already settled

Trap 4: a getter that changes

The engine reads the then property once when it decides whether to adopt. A getter that returns something different each time can produce confusing results, and the specification is deliberate about the single read to keep behavior predictable. Do not define then with side effects.

When to use a thenable yourself

Rarely, and for a specific reason: a builder that runs when awaited, an adapter that wraps a legacy API so that await works on it, or a test double. For everything else, return a real promise from an async function. A native promise is cached, cheaper to await, shows up correctly in debuggers and cannot be re-run by accident.

When you receive something that might be a thenable, Promise.resolve(value) normalizes it once, and from then on you hold a native promise. That is the right thing to do at an API boundary, such as a plugin hook that may return a value, a promise or a library's own object.

Why the design is still there

It is fair to ask why the language keeps a rule that lets any object with a then method masquerade as a promise. The reason is history. Before promises were built in, several libraries offered their own implementations, and a shared convention was the only way to let them work together. The convention was written down, adopted by the standard and then frozen, because changing it would break code that has been running for years. The cost is the odd bug above, and the benefit is that an await on a value from a decade-old library still works today. When you write new code, you can ignore all of this, as long as you remember one habit: never give an ordinary object a method called then.

Share

Keep reading

Flat illustration of an editor window full of code, with the Google Chrome, JavaScript and npm logos.
Browser

script Tags: async, defer and type=module

Compare classic, async, defer and module script tags: when each downloads, when each runs, how order is kept and which one to pick for each kind of script.

6 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.