mardi 31 mai 2016

Why does .json() return a promise if in an object literal?

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

let url = "http://ift.tt/281iWkN";

let iterator = fetch(url);

iterator
  .then(response => {
      return {
          data: response.json(),
          status: response.status
      }
  })
  .then(post => document.write(post.data));
;

post.data returns a promise object. http://ift.tt/1P1X4Ky

However if it is written as:

let url = "http://ift.tt/281iWkN";

let iterator = fetch(url);

iterator
  .then(response => response.json())
  .then(post => document.write(post.title));
;

post here is a standard object which you can access the title attribute. http://ift.tt/281j043

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




Aucun commentaire:

Enregistrer un commentaire