1

Code:

fetch(`https://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key=++++++++++&tags=obama&format=json&extras=url_m&nojsoncallback=true`, {
    method: "GET",
    headers : { 
      'Content-Type': 'application/json',
      'Accept': 'application/json'
     }
  }).then(response => {
    console.log(response.json())
  })

Output:

Promise {_40: 0, _65: 0, _55: null, _72: null}
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34

2 Answers2

4

Add another then:

fetch(...).then(resp => resp.json()).then(data => ...)

Note that fetch will only error on a network errors, if you want it to reject the Promise on e.g. a 500 response you'll have to check the status code and throw:

fetch(url)
  .then(resp => {
     // you'll need to supply the function that checks the status here
     if (http_response_ok(resp.status)) {
       return resp.json();
     } else {
       throw new Error(`Got back ${resp.status}`);
     }
  }).then(data => {
     // happy path
  }).catch(err => {
     // sad path
  });
Jared Smith
  • 19,721
  • 5
  • 45
  • 83
2

You could await the response:

fetch(url, options).then(async (response) => {
  const data = await response.json();
  console.log(data)
})
BigPun86
  • 2,480
  • 2
  • 25
  • 49