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 withPromise.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 receivesundefined. The catch handler finished without an error, so its promise is fulfilled with what the handler returns. A handler that only logs returnsundefined. 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.