On Wednesday, July 9, 2014 3:02 AM, Tab Atkins Jr. <jackalm...@gmail.com> wrote:
On Tue, Jul 8, 2014 at 11:35 AM, Juan Ignacio Dopazo <jdop...@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(...); 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