Command Palette

Search for a command to run...

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

Errors skip ahead, catch recovers, then(ok, fail) misses bugs

The three chains run side by side, one microtask step at a time, so their lines interleave. Chain A's error has to pass through one extra skipped then first, so A prints last. Follow each letter on its own.

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?

Errors skip ahead, catch recovers, then(ok, fail) misses bugs

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

B: catch also sees bugs: TypeError
C: only a later catch sees it: thrown in the success handler
A: caught step 2 failed
A: continues with fallback data