Re: [whatwg] Questions about the Fetch API

On Thu, Jul 10, 2014 at 7:17 AM, Juan Ignacio Dopazo <>

> On Wednesday, July 9, 2014 3:02 AM, Tab Atkins Jr. <>
> wrote:
> On Tue, Jul 8, 2014 at 11:35 AM, Juan Ignacio Dopazo
> <> wrote:
> > - And more importantly, why does fetch() return a Promise<Response>
> instead of a Response?
> Because you aren't allowed to do network fetches sync.  (And if you
> have an async action, returning a Promise for its result is the
> idiomatic way to do it, rather than, say, making the object itself
> asynchronous in some way.)
> That makes sense considering Response is representing the actual response
> from the server. You get the response once you get the first answer from
> the server and that response contains the headers. However, my first
> thought when looking at the API was: why can't I just do
> fetch(url).asJSON().then(...)? I'd say that, while headers will be super
> useful for certain kinds of apps and for stuff like Service Workers, at
> least 80% of XHR requests in the web are about getting data and just ignore
> the headers. So instead of just getting the data, we either have to write
> library code or write something like:
> fetch(url).then(function (response) {
>   return response.body.asJSON();
> }).then(...);
FWIW, with ES6 "fat arrow" expressions it will soon be possible to write:

fetch(url).then(r => r.body.asJSON()).then(...);

And there's a proposal for an "infix eventual operator" [1] in ES7 that
would let you write it as:


... which is still wordier than your proposal, of course.


Would it be to crazy to get fetch(url).asJSON() to work? Either by not
> having fetch() return a promise (and return something that has a function
> that returns a promise for the headers) or by having it return a subclass
> of Promise that contains those utility methods?
> Also, my question about the Request class is because I don't see a way to
> get a writable stream for the body of the request. It'd be nice if we had
> that for uploaders for example.
> Thanks,
> Juan

Received on Thursday, 10 July 2014 17:22:02 UTC