Web Development / JavaScript
Lesson 22 of 30
fetch and JSON
fetch asks a server for data without reloading the page. Use async/await to wait for the answer and .json() to read it.
Key points
- await fetch(url)
- await res.json()
- Check res.ok for errors
async function load() {
const res = await fetch("/api/scores");
if (!res.ok) throw new Error(res.status);
const data = await res.json();
console.log(data);
}Quiz · +10 XP
Which keyword waits for a fetch to finish inside an async function?
Log in to save progress and earn XP.