Step 11
The Fetch API provides a JavaScript interface for making HTTP requests. In its simplest form, its syntax looks like this
fetch('/some/api/endpoint/')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.log(err))
Calling fetch()
returns a promise. We can then wait for the promise to resolve. Once resolved, the response will be available within the promise's then()
method.
Using
fetch()
is an example of retrieving resources asynchronously across the network.
We will explore JavaScript's asynchronous behavior in the near future.
Resources
- MDN web docs Fetch API.
- MDN web docs Using Fetch.
- Flavio's article on The Fetch API.