fetch and Promises

Fetch's Response object .json method always returns a promise and must be passed into a .then() method block in order for the json object to be resolved properly. Otherwise use async await.

so…

fetch(url)
.then(response => response.json())
.then(json => do something…);

works, but not

fetch(url)
.then(response => {
  const foo = response.json();
  const header_thing = response.headers.get(‘some_header’);
  return { foo, header_thing }
})
.then(bar => bar.foo … do something that breaks…);

I did not get this directly from the reference, but here it is anyway: Fetch API.

Scroll to Top