News for hackers. Stuff that matters.
Re:JavaScript async/await implemented in V8 by startling on Monday May 16, 2016 @09:28PM (#11710797)
Is the syntax composable? Can I do

    const json = await (await fetch(https://api.github.com/orgs/facebook')).json();
or do I have to name it?
Re:JavaScript async/await implemented in V8 by bsou on Monday May 16, 2016 @09:29PM (#11710801)
yeah that works, your statement is fine
Re:JavaScript async/await implemented in V8 by startling on Monday May 16, 2016 @09:30PM (#11710804)
nice, thanks!
Re:JavaScript async/await implemented in V8 by dvlsg on Monday May 16, 2016 @10:07PM (#11710940)
You probably can, but you may as well just do this instead:

    const json = await fetch('https://api.github.com/orgs/facebook').then(res => res.json());
Re:JavaScript async/await implemented in V8 by ludamad on Tuesday May 17, 2016 @12:18AM (#11711321)
I think the await code is more explicit, whereas a callback is somewhat ambiguous
Re:JavaScript async/await implemented in V8 by nathancahill on Tuesday May 17, 2016 @12:39AM (#11711376)
I'll be glad to do away with callbacks completely tbh.
Re:JavaScript async/await implemented in V8 by smilekzs on Monday May 16, 2016 @09:32PM (#11710811)
What you asked for is whether `await` is idempotent. No it isn't. The "argument" to `await` is a Promise, and the "return value" of it is the resolved value of the Promise (i.e. `Promise#then`).

EDIT: I missed the `.json()` part, but I suppose it doesn't return a Promise, no?

Re:JavaScript async/await implemented in V8 by tanto on Monday May 16, 2016 @09:38PM (#11710838)
actually .json() returns a Promise. The first fetch only returns HTTP status code and some other details. The content itself is streamed. You can also access the stream if you want too. In general you would rather just call .json() and get the parsed data.

The fetch function can for example also be used to get images. There you would use .blob() and assign that to an image.src with URL.createObjectURL(blob);

See here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/U...

Re:JavaScript async/await implemented in V8 by startling on Tuesday May 17, 2016 @01:40PM (#11715614)
This is totally not what I'm asking, thanks.
Re:JavaScript async/await implemented in V8 by tigershark on Tuesday May 17, 2016 @04:04AM (#11711969)
I don't understand your code. the .json() should be inside the parentheses because you cannot await something that has already been converted from a Promise to a real value. And even in that case that code will be exactly equivalent to the synchronous code because you are awaiting in place the result of the Promise. So, given my very limited knowledge of javascript, I think that probably you can write it moving the .json() inside, but I don't see why you would ever want to do it if you can have the same result without the await boilerplate...

EDIT: got it, you want to do it inside an aync function with some other code after the await, then I think it makes sense in that case.

Re:JavaScript async/await implemented in V8 by neoeldex on Tuesday May 17, 2016 @08:23AM (#11712973)
fetch() returns a promise, which is unwrapped using await. fetch's promise contains a response object, which has a json method, which returns a promise.

  const responsePromise = fetch(https://api.github.com/orgs/facebook');
  const response = await responsePromise;
  const jsonPromise = response.json();
  const json = await jsonPromise;
Re:JavaScript async/await implemented in V8 by nailer on Tuesday May 17, 2016 @04:53AM (#11712120)
Tip: if you use anything other than fetch you'll just get JSON back immediately based on the MIME type, without the unnecessary conversion step.
Re:JavaScript async/await implemented in V8 by tehbeard on Tuesday May 17, 2016 @05:50AM (#11712321)
Those other than fetch()s still do the "unnecessary conversion step". Don't dismiss fetch by the fact it doesn't hand hold or make assumptions.

It should be said that fetch() is quite low level call compared to what we've had before regarding ajax, so it makes sense in larger projects to wrap it to keep any logging, error handling and retry in one place.

Re:JavaScript async/await implemented in V8 by nailer on Tuesday May 17, 2016 @05:54AM (#11712335)
> it doesn't hand hold or make assumptions

Using MIME types isn't hand holding or making an assumption.

Yes, people who need the low level features will wrap it with a series of competing high level libraries. It's a missed opportunity to make a standard, capable high level API though.

Re:JavaScript async/await implemented in V8 by moron4hire on Tuesday May 17, 2016 @08:05AM (#11712865)
Fetch API also doesn't have any means of tracking progress, making it strictly inferior to XmlHTTPRequest
Re:JavaScript async/await implemented in V8 by nailer on Tuesday May 17, 2016 @09:10AM (#11713277)
In fetch's defence it will in future once the Streams API is finalised: https://github.com/whatwg/fetch/issues/21
Re:JavaScript async/await implemented in V8 by neoeldex on Tuesday May 17, 2016 @08:16AM (#11712935)
Fetch does have streaming uploads and downloads, and a quite some other features which doesn't come with xmlHttpRequest. Making it not strictly inferior to XMLHttpRequest
Re:JavaScript async/await implemented in V8 by moron4hire on Tuesday May 17, 2016 @10:08AM (#11713733)
Will have. Future tense. Not present tense.
Re:JavaScript async/await implemented in V8 by mathias on Tuesday May 17, 2016 @03:17PM (#11716480)
Re:JavaScript async/await implemented in V8 by grandalf on Monday May 16, 2016 @11:38PM (#11711222)
yes code like that works.