Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 24 / 34

Promises & rejection paths

01

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.

Promise outcomes and composition tools
Tool or stateMeaningDecision for the caller
PendingThe outcome is not settled yetWait for fulfillment or rejection
FulfilledOne result is availableHandle it with then or await
RejectedOne failure reason is availableHandle it with catch or an awaited try/catch
Promise.allWait for all inputs; reject on an input rejectionUse when all results are required
Promise.allSettledWait for all outcomesInspect success or failure for each input
finallyRun outcome-independent cleanupDo 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.

02

Mental model

FOLLOW THE VALUE

readScore()
pending receipt
future result

The caller receives a promise.

01 / 03Trace the transformation

03

Live example

The first then returns 8 to the next handler. Add braces without return and the next handler receives instead.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Pass the value down the chain

Return the doubled score from the first then handler so the next handler receives 8.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

8

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does then return?
    02What happens when catch returns a normal fallback value?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson