Sub topic 1
Recap: Pending, Fulfilled, Rejected
Every promise combinator in this tutorial returns a promise whose fate depends on the states of other promises. Before looking at Promise.all and the others, this chapter reviews the state model they all depend on.
Prerequisites
This chapter assumes you already know:
- JavaScript functions, arrays, and destructuring
- How to create promises and consume them with
thenandcatch asyncfunctions andawaittry/catcherror handling- The basic idea of the event loop and asynchronous I/O
The Three States
A promise is always in exactly one of three states:
| State | Meaning | What it holds |
|---|---|---|
| pending | The operation has not finished yet | Nothing yet |
| fulfilled | The operation succeeded | A value |
| rejected | The operation failed | A reason, usually an `Error` |
A promise that is either fulfilled or rejected is called settled. The combinators rely on this word. For example, Promise.allSettled waits until every input is settled, regardless of whether each one fulfilled or rejected.
Settlement Happens Once
After a promise settles, its state and its value or reason never change. Any later calls to resolve or reject are ignored without an error:
const p = new Promise((resolve, reject) => {
resolve('first');
reject(new Error('ignored'));
resolve('also ignored');
});
p.then((value) => console.log(value)); // 'first'This rule is what makes combinators predictable. When Promise.race settles with the first input to finish, inputs that settle later cannot change its result.
Resolving Is Not Always Fulfilling
When you call resolve with another promise (or any thenable, meaning an object with a then method), the outer promise follows that inner promise's eventual state. It can still end up rejected:
const inner = Promise.reject(new Error('inner failed'));
const outer = new Promise((resolve) => resolve(inner));
outer.catch((err) => console.log(err.message)); // 'inner failed'Plain values work the other way. Promise.resolve(42) produces a promise that is already fulfilled with 42. The combinators apply this same conversion to every item in their input, which is why you can mix plain values in with promises.
Work Starts When the Promise Is Created
A promise has no "start" button. The executor function runs synchronously as soon as you call new Promise(...), and a function such as fetch begins its request the moment you call it. A promise is only a placeholder for the result of work that is already running.
const p = new Promise((resolve) => {
console.log('executor runs now');
setTimeout(() => resolve('done'), 100);
});
console.log('promise created');
// executor runs now
// promise createdThis affects the next chapter. By the time you pass promises to a combinator, their operations are already running. The combinator only observes them.
Unhandled Rejections
If a promise rejects and nothing ever attaches a rejection handler, Node.js and browsers report an unhandled rejection. In recent Node.js versions, this crashes the process by default. Combinators attach handlers to every input, so a rejected input passed to Promise.all is treated as handled. A promise you create but never await or catch is not.
Exercise
A promise's executor calls resolve(Promise.reject(new Error('x'))) and then calls resolve('y'). What is the promise's final state, and what does it hold?
Show answer
Rejected, with the Error 'x'. The first resolve call locks the outer promise to the inner promise, which rejects. The second resolve call is ignored because the outcome was already decided.
Exercise
Is a fulfilled promise "settled"? Is a pending promise that has been resolved with another pending promise settled?
Show answer
A fulfilled promise is settled. The second promise is not settled. It is locked in to follow the inner promise, but it stays pending until the inner promise fulfills or rejects.