Sub topic 2
What a Promise Is: States and Settlement
This chapter assumes you can write JavaScript functions and arrow functions, know what a callback is, and can run code in Node.js or a browser console. The previous chapter showed how nested callbacks get hard to manage. This chapter explains the object that replaces them.
The Working Model
A promise is an object that stands for the result of an operation that may not have finished yet. You get the promise right away, before the result exists. Later the operation either produces a value or fails with a reason, and the promise records which one happened.
Every promise is in exactly one of three states:
| State | Meaning | Carries |
|---|---|---|
| pending | The operation has not finished | Nothing yet |
| fulfilled | The operation succeeded | A value |
| rejected | The operation failed | A reason (usually an `Error`) |
A promise that is fulfilled or rejected is called settled. "Settled" is a convenient word for "no longer pending".
Settlement Is Permanent
A promise moves from pending to settled once, and then it stays that way. A fulfilled promise never becomes rejected, and its value never changes. Any later attempt to settle it is silently ignored.
const p = new Promise((resolve, reject) => {
resolve('first');
resolve('second'); // ignored
reject(new Error('late')); // ignored, no error is raised
});
p.then((v) => console.log(v)); // firstThis rule is what makes promises reliable compared with raw callbacks. A callback API can, through a bug, call your callback twice, or call both its success and failure paths. A promise cannot report two outcomes. Code that reads the result can trust it.
Permanence also means the time you attach a handler does not matter. If you call .then on a promise that settled an hour ago, your handler still runs with the stored value. You do not miss the event.
const ready = Promise.resolve(42);
setTimeout(() => {
ready.then((v) => console.log('late subscriber got', v)); // late subscriber got 42
}, 1000);You Cannot Read the State Directly
JavaScript has no promise.state property. The only way to get the result is to attach handlers with then or catch, or to use await. Later chapters cover these. Node.js shows the state when you log a promise, which helps with debugging:
const pending = new Promise(() => {}); // never settles
const done = Promise.resolve('ok');
const failed = Promise.reject(new Error('bad'));
failed.catch(() => {}); // handle it so Node does not warn
console.log(pending); // Promise { <pending> }
console.log(done); // Promise { 'ok' }
console.log(failed); // Promise { <rejected> Error: bad ... }Do not write program logic that depends on this output. It is a debugging aid only.
Resolved Is Not the Same as Fulfilled
People often mix up these terms:
- Fulfilled means the promise has a final success value.
- Resolved means the promise's fate is fixed. Either it has settled, or it has been locked to follow another promise.
If you resolve promise A with promise B, A is resolved but still pending. A will copy B's outcome once B settles. That outcome can even be a rejection. The chapter on thenables explains how this works.
Common Errors
- Expecting a value synchronously.
const x = fetchData();gives you a pending promise, not data. LoggingxshowsPromise { <pending> }. - Trying to "reset" a promise. A settled promise cannot be retried. To retry, start the operation again and get a new promise.
- Assuming a second
resolveorrejectthrows. It does nothing, which can hide bugs where code settles a promise too early.
Exercise
What does this code log, and why? const p = new Promise((resolve, reject) => { reject(new Error('A')); resolve('B'); }); p.then((v) => console.log('value', v)).catch((e) => console.log('error', e.message));
Show answer
It logs "error A". The first call, reject, settles the promise as rejected. The later resolve('B') is ignored because a settled promise never changes state. The then handler is skipped and catch receives the error.
Exercise
A promise is resolved with another promise that is still pending. Which state is the outer promise in, and which words describe it?
Show answer
It is pending. It is also "resolved", because its fate is locked to the inner promise, but it is not fulfilled or settled. It will settle with whatever outcome the inner promise eventually has.