I 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…
JavaScript async/await implemented in V8
71–80 of 227 posts
Re: JavaScript async/await implemented in V8
#72Earlier quoted context omitted.
You'd like Tcl and various Perl libraries. Mojo (Perl event loop among other things) in particular has a very nice, straightforward event flow quite reminiscent of Q. async/await looks nice but doesn't scale. As soon as you have to do something other than wait for a callback it falls apart.
> async/await looks nice but doesn't scale. As soon as you have to do something other than wait for a callback it falls apart. Waiting for a callback is what you do 90% of time. For the rest, you can always combine async/wait with Promises...
Re: JavaScript async/await implemented in V8
#73Earlier quoted context omitted.
Just to be picky, but I believe you would be better off with this: async function main () { try { const jsons = await Promise.all([ fetch('https://api.github.com/orgs/facebook'), fetch('https://api.github.com/orgs/facebook') ]).map(promise => promise.then(res => res.json())); } catch (err) { console.log(err) } } This way, if one response was much quicker than the other, you could begin sending it through the `res.jso…
Nit -- you want: async function main () { try { const jsons = await Promise.all([ fetch('https://api.github.com/orgs/facebook'), fetch('https://api.github.com/orgs/facebook') ].map(promise => promise.then(res => res.json()))); } catch (err) { console.log(err) } } As written you get "Promise.all(...).map is not a function"
async.parallel({
facebook: done => request("https://api.github.com/orgs/facebook", done),
twitter: done => request("https://api.github.com/orgs/twitter", done)
}, (err, responses) => console.log(err, responses); );
And output something like: null, {
facebook: { // facebook data },
twitter: { // twitter data }
}
Sure it suffers from a third party dependency and I would argue its slightly less concise, but we're getting to the point of opinionated API's just like some developers prefer promises, some will prefer async/await, others will continue with vanilla callbacks.Re: JavaScript async/await implemented in V8
#74Earlier quoted context omitted.
Would it? IIRC Promise.all will run the two responses in parallel, but will not send the first into the map before both are finished.
Promise.all doesn't "run" anything. Promises "run" the very moment they come into existence. Promise.all simply tells you if and when all of them are settled. It depends on your Promises whether they are doing something outside the single threaded event loop or not for them to be parallel or not.
Which is why I'm a massive fan of Futures from ramda-fantasy[0] instead of Promises for certain tasks!
[0] https://github.com/ramda/ramda-fantasy/blob/master/docs/Futu...
Re: JavaScript async/await implemented in V8
#75http://kangax.github.io/compat-table/esnext/#test-async_func...
Apparently Microsoft Edge seems to have been the first browser to implement it... good job Microsoft!
Re: JavaScript async/await implemented in V8
#76Re: JavaScript async/await implemented in V8
#77Re: JavaScript async/await implemented in V8
#78I 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…
This is speaking from personal experience, albeit not in JS, but rather .NET. However, the story there is similar - .NET used callback-based continuations first (the Begin... pattern), then added promises (Tasks) in .NET 4.0, then finally async/await syntactic sugar in C# 5. We introduced tasks to our codebase pretty soon, but had to avoid await for a while because of the requirement for the code to be compilable with C# 4. When we finally dropped that requirement a couple of years later, and started moving to await, code became much simpler and cleaner as a result, and new code is also much easier to write, with fewer silly mistakes (like "I forgot to check for errors").
Re: JavaScript async/await implemented in V8
#79Re: JavaScript async/await implemented in V8
#80Earlier quoted context omitted.
> async/await looks nice but doesn't scale. As soon as you have to do something other than wait for a callback it falls apart. Waiting for a callback is what you do 90% of time. For the rest, you can always combine async/wait with Promises...
Maybe if the only thing you do is Ajax. Writing a server, a web crawler, a P2P protocol, heterogeneous OpenCL computations, etc, and you're going to need something a little more heavy-duty.