Promise States
Promise States
Section titled “Promise States”Introduction
Section titled “Introduction”A Promise has three states: pending, fulfilled, and rejected. Once a promise settles (fulfilled or rejected), it cannot change state.
Visual Explanation
Section titled “Visual Explanation”flowchart TD A["new Promise(executor)"] B["⏳ Pending"] C["✅ Fulfilled (resolve)"] D["❌ Rejected (reject)"]
A --> B B --> C B --> D C --> E[".then()"] D --> F[".catch()"] E --> G[".finally()"] F --> GState Transitions
Section titled “State Transitions”const promise = new Promise((resolve, reject) => { // Promise starts as PENDING
// After resolve() → FULFILLED resolve('Success');
// After reject() → REJECTED // reject(new Error('Failed'));
// Once settled, further resolve/reject calls are ignored resolve('This is ignored'); // Already resolved above});
console.log(promise); // Promise { 'Success' }Checking State
Section titled “Checking State”// You can't directly check a promise's state from user code// But you can observe it through callbacks:
const p = new Promise((resolve) => { setTimeout(() => resolve('done'), 1000);});
console.log(p); // Promise { <pending> } — still pending
p.then(value => { console.log(value); // 'done' — now fulfilled});Summary
Section titled “Summary”- Pending: Initial state, not yet settled
- Fulfilled: Operation completed successfully (resolve called)
- Rejected: Operation failed (reject called)
- Once settled, state is immutable