Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 10.6 — Chaining and Error Handling with Promises
JavaScript · Example 2 of 4

Forgetting return breaks the chain

The first chain didn't wait for its inner request, so it finished early with undefined. In real code that's a race: data shows up "sometimes".

Every then, catch and finally returns a new promise, so you can chain steps: what a callback returns becomes the next step's value (and a returned promise is waited for), a throw skips ahead to the nearest catch, and a rejection nobody handles is reported as an unhandled rejection.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Rewrite the callback-hell example from Topic 10.4 as a flat chain, using later(value) that returns a promise fulfilled after 50 ms.

02

Write a chain that starts from Promise.reject(new Error("offline")), recovers with the value "cached copy" in a catch, and logs Showing: cached copy.

Explain it without notes

01

What does then return, and how is its value decided?

02

How do errors travel through a promise chain, and how do you recover from one?

03

What's the difference between p.then(ok, fail) and p.then(ok).catch(fail)?

04

What is an unhandled rejection, how is it reported, and how do you avoid it?

Forgetting return breaks the chain

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Without return: undefined
With return: 5