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