Fetching data from an API
Lesson 17 of 20 · Web.Micro
`fetch` asks a server for data over the network; the answer arrives later, as JSON.
fetch("/api/users") returns a Promise: a box that will hold the answer when the network replies. .then(r => r.json()) reads the body as JSON. Notice the logs: "after fetch" prints first. (This page has a small practice API.)
Check yourself
What does fetch return right away?
Do it in the browser: a real HTML, CSS and JavaScript tool, a short mission and instant feedback. No install.
Start this lesson — free