Turn a response into data
You ask a server for data and get an answer back. But the answer is not the data
yet. You are holding a Response: the status and headers, plus a body still
waiting to be read. Reach inside and you find a stream, not the array you wanted
on screen.
To get the value, you decode the body once it arrives. When the server answers
with JSON, response.json() is the method you reach for:
const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=3');
const data = await response.json();
Two awaits, two different waits. The first returns when the response headers
arrive; the second waits for the body to be read and parsed. response.json()
returns a promise of its own, so it needs that second await — leave it off and
you hold a pending promise, not your data.
PokeAPI answers a list request with { results: [{ name }, …] }, so after both
awaits data.results is the array you can render. No separate parse step and no
.then chain required.
Export fetchPokemonList from index.js. Your job is to fetch a URL and
return the parsed JSON body.
Pokémon and Pokémon character names are trademarks of Nintendo.
Challenge
A server hands back a Response, but the data you wanted is still locked in an
unread body. Export an async function fetchPokemonList from index.js. It
takes a URL, calls fetch, then returns the parsed JSON body via
await response.json(): the data the server sent, not the Response object.
The Test stubs fetch and expects the returned value to equal the stub's JSON
payload.
Pokémon and Pokémon character names are trademarks of Nintendo.