05. Thenable
“thenable” is an object or function that defines a
then
method that follow Promise specification.
A thenable is an object that has a Promise-style then()
method. Whenever the API is only interested in being notified of settlements, it only demands thenables (e.g. the values returned from then()
and catch()
; or the values handed to Promise.all()
and Promise.race()
).
// Resolving a thenable object
const p1 = Promise.resolve({
then(onFulfill, onReject) {
onFulfill("fulfilled!");
}
});
console.log(p1 instanceof Promise); // true, object casted to a Promise
p1.then(
value => {
console.log(value); // "fulfilled!"
},
e => {
// not called
}
);
Last updated
Was this helpful?