Sikau

JavaScript · Day 54 · about 30 min

fetch and APIs

Real data from the internet.

Talking to the internet

Weather apps, maps, news feeds — they all ask other computers for data. Those computers offer an API: a set of web addresses you can request data from. JavaScript asks with fetch.

fetch with async/await

async function getJoke() {
  const response = await fetch("https://official-joke-api.appspot.com/random_joke");
  const joke = await response.json();
  console.log(joke.setup);
  console.log("…" + joke.punchline);
}

getJoke();

Sikau's practice space can only fetch from a few friendly practice APIs, like dummyjson.com and official-joke-api.appspot.com.

The rest of this lesson

  • Checking for problemsexplain
  • What a request really looks likeexplain
  • Predictpredict
  • Fetching safelyorder
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript