what's the point of explicitly declaring functions "async", and then explicitly saying "await". IMHO all this could be done automatically by language.
Because you don't always have to await a function, and it isn't always possible for the runtime or language to decide whether to "await" or not. /* The following code may or may not use an await. Not await-ing would be better user experience. */ async function sendEmails(id) { ... } async function signUp(userData) { const user = await db.saveUser(userData); sendEmails(user.id); //
JavaScript async/await implemented in V8
81–90 of 227 posts
Re: JavaScript async/await implemented in V8
#82I have yet to see a convincing argument that this feature is necessary or even helpful beyond one-liners. The Q promises API, to me, is the right way to reason about asynchrony. Once you understand closures and first class functions, so much about complex asynchronous flows (e.g. multiple concurrent calls via Q.all, multiple "returns" via callback arguments) become so simple. The "tons of libraries" argument doesn't…
I agree with you, I feel this way about async/await and felt similar about ES6 classes ans various other features. Perhaps one advantage is it makes the language "look" more appealing to beginners, almost all of whom are familiar with object-oriented programming and imperative programming. In the end, as you pointed out, these construct may work against developers in large/concurrent codebases and is an undoing of th…
C++0x started adding the kitchen sink and that's when C++ jumped the shark. JS seems to be headed in the same direction...
Re: JavaScript async/await implemented in V8
#83Earlier quoted context omitted.
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?
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
#84Thank you to Microsoft and Anders Hejlsberg for inventing Async/Await (and to F# for the original inspiration) https://en.wikipedia.org/wiki/Futures_and_promises
(Although, just to be pedantic, the concept of futures is way older than F#. Alice's syntax, for example, is pretty close)
Re: JavaScript async/await implemented in V8
#85I have yet to see a convincing argument that this feature is necessary or even helpful beyond one-liners. The Q promises API, to me, is the right way to reason about asynchrony. Once you understand closures and first class functions, so much about complex asynchronous flows (e.g. multiple concurrent calls via Q.all, multiple "returns" via callback arguments) become so simple. The "tons of libraries" argument doesn't…
The think about using async/await its to write less code, make it much simple and makes also easy to try/catch errors. The think I don't like about promises its that you need to write a lot of lines that you could avoid with async/await, i.e: new Promise(function(success, failure){ db.fetch('sql...', function(err, data){ if (err) failure(err) else success(data) }) }) VS var data = await db.fetch('sql...') Also if you…
Re: JavaScript async/await implemented in V8
#86Earlier quoted context omitted.
Is using async/await through babel a good idea? When I last looked at it I thought it was using a busy loop which didn't seem like such a great idea to use in shipping code.
Async/await is just syntax-sugar around Promises. No busy loops involved. (Those generally don't work in Javascript anyway.)
Re: JavaScript async/await implemented in V8
#87I'm definitely rooting for the feature to be included in the spec as soon as possible, but I'm a little weary when features are added to the engine before being standardized. Object.observe, anyone?
Re: JavaScript async/await implemented in V8
#88Realistically does this mean we will see async/await in node-v7.0?
Probably in an upcoming Node 6 -- they do update the running version with the latest v8 releases IIRC.
Re: JavaScript async/await implemented in V8
#89I have yet to see a convincing argument that this feature is necessary or even helpful beyond one-liners. The Q promises API, to me, is the right way to reason about asynchrony. Once you understand closures and first class functions, so much about complex asynchronous flows (e.g. multiple concurrent calls via Q.all, multiple "returns" via callback arguments) become so simple. The "tons of libraries" argument doesn't…
> (e.g. multiple concurrent calls via Q.all, multiple "returns" via callback arguments) How about: async function main () { try { const responses = await Promise.all([ fetch('https://api.github.com/orgs/facebook'), fetch('https://api.github.com/orgs/facebook') ]); const jsons = await Promise.all(responses.map(res => res.json())) } catch (err) { console.log(err) } } I think this is pretty clear and not needing any lib…
There is, of course, no library needed to use pure promises for the same functionality, though.
My point is that all of this is possible without async/await, which to me are abstractions that cloud the landscape, obscuring the real evented nature of JavaScript that makes async programming so easy, IMO.
Re: JavaScript async/await implemented in V8
#90Realistically does this mean we will see async/await in node-v7.0?
I hope the node APIs are changed/extended to return promises eventually so I don't have to keep on using promise wrappers for all of them.