Chapter XVII · Promises
Promise States
A Promise always exists in one of three mutually exclusive states: Pending, Fulfilled, or Rejected.
01 How It Works
Think of a shipped package tracking status: Pending transit, Delivered (Fulfilled), or Returned (Rejected).
Once settled (either fulfilled or rejected), a promise's state cannot change, and its immutable result value is locked in.
example.js
const p = Promise.resolve("Done");
// State: Fulfilled, Value: "Done"
1. Initial state is **Pending** while async work runs.
2. State transitions to **Fulfilled** upon successful resolution.
3. State transitions to **Rejected** if an error occurs.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
const samplePromise = new Promise((res, rej) => setTimeout(res, 100, "Ready"));
Key Takeaway
Promises are immutable once settled into either a fulfilled or rejected state.