×

javascript · level 3 · unit 5 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

links steps together; handles a failure above it.

Before await existed, code used .then: promise.then((value) => ...) runs a function once the promise resolves. It returns a new promise, so you can add another .then, building a chain. Each step receives whatever the step before it returned.

If any step throws or rejects, the steps after it are skipped and the nearest .catch runs, like catch after try. .finally runs at the end either way. You will meet chains in older code, so learn to read both styles.

const url = "https://api.example.com/menu";
await fetch(url)
  .then((reply) => reply.json())
  .then((menu) => menu.filter((dish) => dish.vegan))
  .then((vegan) => console.log(vegan.map((dish) => dish.name)))
  .catch((error) => console.log("Menu failed:", error.name))
  .finally(() => console.log("Done"));

Gwecode answers requests to https://api.example.com with a practice server, so these lessons never need the real internet.