Skip to the workspace
Pebellslearn by building
Asynchronous JavaScript  ›  Lesson

Compose Promise Outcomes

Promises 20 min

javascript / Compose Promise Outcomes

Explore each scene at your own pace.

01 · Compose Promise Outcomes

Compose Promise Outcomes

Compose Promise Outcomes

Promise composition combines several eventual outcomes into a policy: require all successes, inspect every settled outcome, take the first settlement, or take the first fulfillment. The composition method does not automatically cancel other work.

Decide whether one failure should block the whole screen or whether successful panels can still be shown.

Choose a failure policy before choosing a method

A dashboard loads article titles and an optional recommendation panel. If the titles fail, the main page cannot do its job; a failed recommendation may be recoverable. Whether a failure is fatal is a product rule. Promise composition implements that rule; it does not decide it for you.

Method Resolves when Rejects when Empty input
Promise.all Every input fulfills An input rejects Fulfills with []
Promise.allSettled Every input settles Input rejections become result records Fulfills with []
Promise.race The first input settles successfully The first settlement is rejection Remains pending
Promise.any An input fulfills All inputs reject, with AggregateError Rejects with AggregateError
Walk through the modelCompose Promise OutcomesAuthored snapshots of the example; use Run below to test the code.
article Apromise A
recommendationspromise B
article Cpromise C
Each invocation supplies an eventual outcome.
1 of 3
javascript

The callbacks produce two fulfilled titles and one rejected outcome. allSettled preserves input order in its result, regardless of settlement order. Every result has a status; access value only for a fulfilled result and reason for a rejected one. Promise.all also preserves input order for its fulfilled array. It does not serialize independent operations into one-at-a-time work.

Start times and await placement

Calling an async operation usually starts or initiates it before you put its returned promise into an array. await loadA(); await loadB(); starts B after A completes. Creating both promises first and then awaiting Promise.all can allow independent operations to proceed concurrently. Concurrency is not necessarily parallel JavaScript execution on several threads.

A rejection from Promise.all or a winner from race does not cancel the other requests or timers. A timeout implemented with race can stop waiting while the original request keeps working. Use the host's cancellation facility where available, and keep a stale-result guard when relevant.

Catch and finally do different jobs

A catch handler can recover by returning a value or keep the rejection by throwing. A finally callback runs on either settlement for cleanup. Returning an ordinary value from finally does not replace the earlier result, but throwing or returning a rejected promise can replace it with a rejection. Keep cleanup reliable; do not accidentally hide the original failure.

Predict, change and check

Replace allSettled with all and explain which caller must catch the rejection. Try an empty input. In the task, unsuccessful panels are intentionally omitted, while successful titles keep their original order. The requirement is partial recovery, not 'ignore every error' as a universal policy.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.