liveBook · Manning

Thenable

As explained in Secrets of the JavaScript Ninja, Third Edition

A thenable is an object with a then method whose behavior can be adopted by a promise.

A thenable is an object with a `then` method. When a promise’s `resolve` function receives one, the promise adopts its behavior: it follows the thenable’s outcome instead of immediately fulfilling with the object itself. Since promises have a `then` method, one promise can adopt another. section 8.1.1

Keep resolution and settlement distinct:

  • A promise is settled only when fulfilled or rejected; once settled, its state and value cannot change. section 8.1.1
  • Calling `resolve` with a thenable does not guarantee immediate settlement. If its `then` method never calls a supplied callback, the adopting promise remains pending. section 8.1.1
// Creates a promise that’s permanently pending.
new Promise((resolve) =>
  resolve({
    then: () => {},
  }),
);

This promise adopts a thenable whose `then` method calls no callbacks, so it remains pending. section 8.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Distinguishes resolution from settlement and shows that adoption can leave a promise pending.
Examples
Uses promises adopting fulfilled and rejected promises, plus a thenable whose `then` method calls no callbacks.
What you can do after
Understand how resolving with a thenable can make a promise mimic another promise or remain pending.
How it connects
Connects thenables with promises, fulfillment, rejection, and settlement.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.