W3C home > Mailing lists > Public > whatwg@whatwg.org > July 2014

Re: [whatwg] Questions about the Fetch API

From: Joshua Bell <jsbell@chromium.org>
Date: Thu, 10 Jul 2014 10:21:38 -0700
Message-ID: <CAD649j721eAAm9-d-bTN2dy8s6=x_yUxZpV2jHNiBNfaeG6-0A@mail.gmail.com>
To: Juan Ignacio Dopazo <jdopazo@yahoo-inc.com>
Cc: "whatwg@lists.whatwg.org" <whatwg@lists.whatwg.org>, "Tab Atkins Jr." <jackalmage@gmail.com>
On Thu, Jul 10, 2014 at 7:17 AM, Juan Ignacio Dopazo <jdopazo@yahoo-inc.com>
wrote:

>
>
> On Wednesday, July 9, 2014 3:02 AM, Tab Atkins Jr. <jackalmage@gmail.com>
> wrote:
>
>
>
>
>
> On Tue, Jul 8, 2014 at 11:35 AM, Juan Ignacio Dopazo
> <jdopazo@yahoo-inc.com> 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:

fetch(url)!body.asJSON().then(...);

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

[1] http://wiki.ecmascript.org/doku.php?id=strawman:concurrency

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

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 17:00:21 UTC