Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow a Failure Down the Chain

In this lesson, you pointed the Adeyemi family’s planning script at Tunde’s pick, which isn’t on view, and followed the failure down the chain to one .catch() at the end.

Key Points

  • The kiosk stand-in’s findArtwork() now checks whether it has the ID before it waits. If it doesn’t, it gives back a rejected promise, made with Promise.reject(), with an error saying the artwork isn’t on view. It’s a toy, but it’s enough to follow a failure.
  • With Tunde’s pick, the chain prints only Couldn't plan the visit: Artwork 16568 isn't on view. The title and gallery lines never print.
  • The file’s promise is fulfilled, and the first two handlers hand on Tunde’s line, then his ID. The third handler returns the kiosk’s promise, which is rejected, so the promise from that step follows it and is rejected with the same error.
  • Each of these .then() handlers is only for a fulfilled promise. When the promise it’s attached to is rejected, JavaScript skips the handler, and the new promise from that step is rejected with the same error. The rejection moves down, step by step, and the gallery step is skipped the same way.
  • The first handler for a rejection is the .catch() handler at the end. It receives the error and logs its message.
  • Like .then(), .catch() gives back a new promise. A .then() added after the catch runs and receives undefined. The catch handler finished without an error, so its promise is fulfilled with what the handler returns. A handler that only logs returns undefined. After this catch, which only logs, the chain is back on the happy path.

As a rule, a rejection skips every .then() handler for a fulfilled promise below it, down to the nearest .catch(). After a catch that only logs, the chain carries on. So, as long as each step returns its promise, one .catch() at the end covers every step above it.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your planning script now looks up a pick and its gallery in one flat chain. Each handler returns its result, so each step waits for the one before.

00:10 But the kiosk is old, and not every artwork is on view. Tunde’s pick, 16568, is away today. So the kiosk stand-in gets one change. Before it waits, findArtwork() checks whether it has the ID.

00:26 If it doesn’t, it gives back a rejected promise, made with Promise.reject(), with an error saying the artwork isn’t on view. It’s a toy, but it’s enough to follow a failure.

00:40 Now point the chain at Tunde. The first handler now takes Tunde’s line, the last one, instead of Tolu’s. And at the end of the chain, a .catch() logs a message if something goes wrong, as you wrote it for a missing file.

00:55 The chain has five handlers for a fulfilled promise and one for a rejection. Your turn: pause the video and predict which lines the terminal will show.

01:11 Only one line prints: Couldn’t plan the visit, because artwork 16568 isn’t on view. No title and no gallery. To see why, follow the promises down the chain.

01:25 The file’s promise is fulfilled with the text. The first two handlers run, and hand on Tunde’s line, then his ID. The third handler returns the kiosk’s promise, and that one is rejected.

01:39 So the promise from this step follows it, and is rejected with the same error.

01:45 The next step only has a handler for a fulfilled promise. The promise it’s attached to is rejected, so JavaScript skips the handler. The new promise from this step is rejected with the same error, and the rejection moves down.

02:00 The gallery step is skipped the same way. The first handler for a rejection is the catch handler at the end. It receives the error and logs its message.

02:12 What about the catch’s promise? Like .then(), .catch() gives back a new promise too. To see what it holds, add one more .then() after the catch, logging what it receives.

02:25 Run the script again, and the new handler runs. It receives undefined from the catch, and logs it after the label.

02:34 That undefined comes from the catch. Its handler finished without an error, so the catch’s promise is fulfilled. It holds what the handler returns, and a handler that only logs gives back undefined, because it returns nothing.

02:49 So after this catch, which only logs, the chain is back on the happy path.

02:56 Here’s the rule. A rejection skips every handler for a fulfilled promise below it, down to the nearest catch handler. After a catch that only logs, the chain carries on.

03:08 So, as long as each step returns its promise, one catch at the end covers every step above it, and Tunde gets a clear message. The kiosk’s real lookup still takes a callback, though.

03:20 Next, in Wrap a Callback API in a Promise, you’ll make a promise of your own for it.

Become a Member to join the conversation.