CurriculumWeb development · Week 04

Week 04 / HTTP · JSON · fetch · loading and error states

Connect to an API.

The idea

An API lets one program request data from another. A GET request asks for information; JSON is a common format for the response. Real requests take time and can fail, so a useful interface shows loading, success, and error states.

Request JSON and handle failure

async function loadData() {
  setStatus("Loading…");
  try {
    const response = await fetch(dataUrl);
    if (!response.ok) throw new Error("Request failed");
    const result = await response.json();
    showResult(result);
  } catch {
    setStatus("Could not load data. Try again.");
  }
}

Build it

Work through these steps in your own project. Check them off as you go; your checklist stays in this browser.

What should a person see when the network is slow or the API is unavailable?

Notes save in this browser.