Thenable assimilation and promise resolution
How it works
A thenable is an object or function with a callable then. Promise resolution reads that method and arranges a job to invoke it with resolving functions. It adopts the resulting outcome, including nested thenables. A throwing then getter rejects the promise; self-resolution rejects with TypeError. Treat unfamiliar thenables as code that may fail, not as passive data.
Example 1
const value = { then(resolve) { console.log("then called"); resolve(6); } };Promise.resolve(value).then(console.log);console.log("sync");Expected output in the tested environment:
syncthen called6The then method is invoked asynchronously by a resolution job.
Example 2
const value = { get then() { throw new Error("getter failed"); } };Promise.resolve(value).catch(e => console.log(e.message));Expected output in the tested environment:
getter failedReading the then getter fails and the promise rejects.
Common mistake
Do not assume that resolving with an object simply stores the object unchanged; a callable then may be assimilated.
Practice
Build a thenable that calls resolve twice and then throws. Predict the outcome and test it locally.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Thenable assimilation and promise resolution — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
What qualifies an ordinary object as a thenable for resolution?
What occurs when a promise is resolved with itself?
What if reading a then property throws?
For Thenable assimilation and promise resolution, what is the console output of example 1 in Node.js 22.16.0?
const value = { then(resolve) { console.log("then called"); resolve(6); } };Promise.resolve(value).then(console.log);console.log("sync");For Thenable assimilation and promise resolution, what is the console output of example 2 in Node.js 22.16.0?
const value = { get then() { throw new Error("getter failed"); } };Promise.resolve(value).catch(e => console.log(e.message));