Skip to the workspace
Pebellslearn by building
JavaScript  ›  Capstone Project

Fetching Data with async and await

JavaScript 16 minfetch returns a promise for a response, so an async function awaits it, checks response.ok because HTTP errors do not reject, awaits the body, and handles failures with try and catch.

frontend / Fetching Data with async and await

Explore each scene at your own pace.

01 · The rule

The rule

Fetching Data with async and await

Until now the bakery's menu has been written into the HTML. The owner would rather update prices in one place and have the page load them fresh. The page can ask the server for the menu as data, using the same HTTP conversation from the first section, but from JavaScript. The catch: the answer takes time, and the page must not freeze while it waits.

The rule

fetch promises a response: await it, check ok, then read the body.

async function loadMenu() {
  try {
    const response = await fetch("/api/menu");
    if (!response.ok) {
      throw new Error(`Menu request failed: ${response.status}`);
    }
    const items = await response.json();
    showMenu(items);
  } catch (error) {
    showMessage("Sorry, the menu could not load. Please try again.");
  }
}

Promises. fetch does not return the response directly. It returns a promise, an object standing for a result that will arrive later. The rest of the page keeps working in the meantime: scrolling, clicking, animations.

await pauses this function only until the promise settles, then hands you the value. It can only be used inside a function marked async (or at the top level of a module). An async function always returns a promise itself.

Two awaits. The first await fetch(...) gives you the response once its status and headers arrive. The body may still be downloading, so reading it is another promise: await response.json() for JSON, or await response.text() for plain text.

Checking ok. Here is the surprise: fetch only rejects when the request could not be made at all, such as no network. A 404 or 500 is still a response, so the promise resolves normally. response.ok is true only for status codes from 200 to 299, so you must check it yourself.

Errors. Wrapping the work in try...catch catches both kinds of failure: a network error from fetch, and the error you throw for a bad status. Then you show the visitor a helpful message instead of a blank space.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.