JS: Promise Thenable Object

By Xah Lee. Date: . Last updated: .

What is thenable object?

A thenable object is object with a then method.

you can think of it as same as promise object but not standard.

object created by the standard builtin Promise is also a thenable.

the idea of thenable object is for backward compatibility with JavaScript libraries that implements async before the real promise object in ECMAScript 2015. These libraries, their promise-like feature all have a then method. ECMAScript 2015 made it compatible with them so that a object with then method can also be considered as a promise object.

Example

For example, this is a thenable object:

// a thenable object.
// this thenable never settles.
const xx = { then: (aa, bb) => 3 };

// a thenable object.
// this thenable resolves to fulfilled with value 3.
const x1 = { then: (aa, bb) => aa(3) };

// a thenable object.
// this thenable resolves to rejected with value 4.
const x2 = { then: (aa, bb) => bb(4) };

Thenable behavior

thenable's argument type requirement is the same as the executor in new Promise(executor).

in a thenable object:

{ then: (resolve, reject) => {…} }

the functions resolve, reject act the same way as in executor.