> For the complete documentation index, see [llms.txt](https://asad-razvi.gitbook.io/javascript-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://asad-razvi.gitbook.io/javascript-cookbook/asynchronous-recipes/how-do-i-use-fetch.md).

# How do I use fetch?

```javascript
const url = 'https://swapi.co/api/people/5'

const onValidate = (response) => {
  const { status, statusText } = response
  if (status < 200) throw Error(statusText)
  if (status > 299) throw Error(statusText)
  return response.json()
}

const onSuccess = (json) => {
  console.log(`success: ${JSON.stringify(json)}`)
}

const onFailure = (error) => {
  console.error(`error: ${error}`)
}
```

```javascript
fetch(url)
    .then(onValidate)
    .then(onSuccess)
    .catch(onFailure)
```
