Skip to content

Why does .json() return a promise?

I’ve been messing around with the fetch() api recently, and noticed something which was a bit quirky.

let url = "";
let iterator = fetch(url);
  .then(response => {
      return {
          data: response.json(),
          status: response.status
  .then(post => document.write(;
; returns a Promise object.,output

However if it is written as:

let url = "";
let iterator = fetch(url);
  .then(response => response.json())
  .then(post => document.write(post.title));

post here is a standard Object which you can access the title attribute.,output

So my question is: why does response.json return a promise in an object literal, but return the value if just returned?


Why does response.json return a promise?

Because you receive the response as soon as all headers have arrived. Calling .json() gets you another promise for the body of the http response that is yet to be loaded. See also Why is the response object from JavaScript fetch API a promise?.

Why do I get the value if I return the promise from the then handler?

Because that’s how promises work. The ability to return promises from the callback and get them adopted is their most relevant feature, it makes them chainable without nesting.

You can use

fetch(url).then(response =>
    response.json().then(data => ({
        data: data,
        status: response.status
).then(res => {

or any other of the approaches to access previous promise results in a .then() chain to get the response status after having awaited the json body.