⛓️
Promise Internals — How then() Enters the Microtask Queue
Promise's 3 states and the execution mechanism of thenable chaining
State Transitions
pending → fulfilled (resolve called), pending → rejected (reject or throw). Once transitioned, immutable.
then Returns a New Promise
Each .then() returns a new Promise, enabling chaining. Each callback enters Microtask Queue when the previous Promise fulfills.
async/await Relationship
await is syntactic sugar for Promise.then(). Code after await becomes a then() callback. Internally implemented as generator + Promise combination.
Key Points
1
new Promise(executor) runs the executor synchronously immediately
2
resolve() call → pending → fulfilled transition (once, immutable)
3
Callbacks registered via then() enter Microtask Queue
4
async/await is syntactic sugar for Promise + generator
Use Cases
API call chaining — understanding fetch().then().then().catch()
Promise.all/race/allSettled — parallel async control