Promises & rejection paths
Understand the idea
A loading state never clears after a request fails. The success exists, but the returned has no rejection handler.
A receipt for an outcome
Nested s become hard to read when each step must wait for the previous result. Promises give each step a returned value to connect to, so the success path and failure path can stay understandable. They do not guarantee that the operation will succeed.
Pending, fulfilled, or rejected
Callbacks become difficult to follow when each successful operation starts another nested operation with separate failure handling. Promise chaining expresses those dependencies in a sequence. A then handler transforms a fulfilled value, and the promise returned by then represents what happens next.
Settled means fulfilled or rejected. A promise settles only once, so a single promise cannot represent every later click in an interface. Its then callback runs even when its result is already available. Creating a Promise with an executor runs that executor immediately; a promise is not automatically a background thread.
| Tool or state | Meaning | Decision for the caller |
|---|---|---|
| Pending | The outcome is not settled yet | Wait for fulfillment or rejection |
| Fulfilled | One result is available | Handle it with then or await |
| Rejected | One failure reason is available | Handle it with catch or an awaited try/catch |
| Promise.all | Wait for all inputs; reject on an input rejection | Use when all results are required |
| Promise.allSettled | Wait for all outcomes | Inspect success or failure for each input |
| finally | Run outcome-independent cleanup | Do not use it as a replacement for handling failure |
Chaining the next step
Returning matters. If a handler starts another asynchronous operation, return its promise so the chain waits and adopts its outcome. Without that return, the chain can finish while the inner operation is still running, and its rejection may escape your handler.
Every then call returns another promise. Returning a plain value fulfills the next step with that value. Returning another promise makes the chain wait for its outcome. Throwing rejects the next promise. These rules let you read the chain as connected operations rather than unrelated callbacks.
Let failures reach a handler
Throwing inside a then handler rejects the promise it returns. A catch handles a rejection and also returns a promise. Returning an ordinary value from catch recovers the chain; throwing again keeps it rejected. Choose whether the next step should see a fallback or a failure instead of swallowing every error.
Promise work still needs scheduling
Use finally for cleanup regardless of outcome. Promise.all combines operations when you need all their results; one rejection rejects the combined promise but does not cancel the other operations. A promise is not itself cancellation and does not imply parallel CPU work.
At the end of an operation, decide who owns any failure. A catch can show recovery or rethrow for a caller to handle. If a catch returns an ordinary value, the following chain continues as fulfilled with that value. That is useful recovery only when the value is deliberate, not when it quietly hides a failed save.
Mental model
FOLLOW THE VALUE
The caller receives a promise.
01 / 03Trace the transformation
Live example
The first then returns 8 to the next handler. Add braces without return and the next handler receives instead.
Your console output appears here.
Guided challenge
Pass the value down the chain
Return the doubled score from the first then handler so the next handler receives 8.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
8
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.