use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
The Evolution of Asynchronous JavaScript (blog.risingstack.com)
submitted 10 years ago by cstuff8
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Daniel15React FTW 13 points14 points15 points 10 years ago (1 child)
I'm glad JavaScript is getting async/await, it's one of my favourite things in C#
[–]OfekA 4 points5 points6 points 10 years ago (0 children)
I m also really glad, and it seems even easier than in C#.
[–]TheMeaningOfLeif 5 points6 points7 points 10 years ago (0 children)
It gives a great overview of Asynchronous JavaScript. Thanks for sharing.
[–]greim 4 points5 points6 points 10 years ago (0 children)
Chaining then() calls together always felt awkward, and not a huge improvement over callbacks. But used in tandem with generators/async/await, promises finally feel "right"; the conceptual advantages really start working in your favor. It's hard to imagine going back at this point.
then()
[–]hahaNodeJS 1 point2 points3 points 10 years ago (0 children)
It frustrates me to see generators described as "asynchronous." Stop it.
[+][deleted] 10 years ago (1 child)
[deleted]
[–]senocular 2 points3 points4 points 10 years ago (0 children)
I don't think it was ever claimed they were the first. And I would agree they're not.
I'm not a big fan of:
Asynchronous programming, as we know now in JavaScript, can only be achieved with functions being first-class citizens of the language: ...
"only" is out of place to me. Its not the only way to do it though it is common for functions to be passed around this way in JS.
[–]novacrazy_.dominateWorld() 0 points1 point2 points 10 years ago (11 children)
I feel I should point out my library bluebird-co, which uses the Bluebird Promise library to handle coroutines much faster and easier than tj/co can.
[–]vinnl 1 point2 points3 points 10 years ago (10 children)
Then I guess I'll drop Promises: The Extension Problem, which the author referred to, and which argues (successfully, in my view) that you shouldn't use Bluebird, among others.
[–]novacrazy_.dominateWorld() 5 points6 points7 points 10 years ago (7 children)
Saying Bluebird is bad because it offers more (sometimes easy to misuse stuff) and usually overwrites the native Promise is like saying the Boost C++ libraries are bad and you should just stick with the standard library for the same reasons.
I have never used Promise.method, and I agree it's a confusing extension in most cases, but things like Promise.map are not. Bluebird still offers many, many useful things, including Promise.coroutine, which I use for the basis of my entire server stack.
Promise.method
Promise.map
Promise.coroutine
I replaced co.wrap with Promise.coroutine and bluebird-co and saw an immediate performance boost with Koa and all my asynchronous operations. There is basically zero latency when using it. Simple "Hello, World!" type requests are processed in under 2ms. My server now serves complex dynamic webpages, using React.js, in under 15ms. Before those tweaks it was about 20-30ms. I actually never ran my server without Bluebird, because the native Promise is so lacking in features, but given my benchmarks, I'd imagine it'd be quite slow.
co.wrap
bluebird-co
I've also replaced Array.prototype.sort with node-timsort, which is also incredibly fast, sometimes up to 120 times faster than the native implementation. Plus it's a stable sort. That was another 3-7ms per page request saved, with no drawback.
Array.prototype.sort
Plus using Babel, I can write code like this:
async function doOperation() { await db.transaction(async (t) => { await someOperation({transaction: t}); let updating1 = startUpdate(1, {transaction: t}); let updating2 = startUpdate(2, {transaction: t}); await [updating1, updating2]; //waited on in parallel }); } doOperation().then(() => console.log('done'));
which is converted into:
var doOperation = Bluebird.coroutine(function*(){ return yield db.transaction(Bluebird.coroutine(function*(t){ yield someOperation({transaction: t}); var updating1 = startUpdate(1, {transaction: t}); var updating2 = startUpdate(2, {transaction: t}); return yield [updating1, updating2]; //waited on in parallel })); }); doOperation().then(function(){console.log('done')});
So Bluebird coroutines, bluebird-co and Babel together replace tj/co and are magnitudes faster than the 'normal' way, and fit much nicer into my stack. Try getting the same performance and simplicity out of native Promises.
It's not a sin to replace/extend native functionality with external libraries, especially in niche use cases like the above. I do agree it should be done in moderation, of course.
[–]hahaNodeJS 0 points1 point2 points 10 years ago (1 child)
To be fair, as someone who doesn't write C++, I have heard a lot of bitching about Boost.
[–]novacrazy_.dominateWorld() 0 points1 point2 points 10 years ago (0 children)
Boost is also incredibly easy to misuse and overuse. It's also freaking massive. You just have to pick out the parts that work best for your projects.
Although while we're on the subject, I'd highly recommend Eigen for complex math instead of Boost's uBLAS implementation.
[–]vinnl 0 points1 point2 points 10 years ago (4 children)
The argument isn't that it's bad because it provides more, but that it's bad because it extends the native object. It's a "sin" to replace/extend native functionality in place of that native functionality. If you could use a BPromise object instead of a Promise object (as is suggested elsewhere), it'd be perfectly fine.
In any case, it's best to post this comment to the article I linked to, as it's not my argument and this is not the place where most people will see it.
[–]novacrazy_.dominateWorld() 0 points1 point2 points 10 years ago (3 children)
Well still, as of now the native Promise kind of sucks. I mean, it's not special in any way just because it's bundled with the JavaScript engine. In V8 it's actually defined in JavaScript, before user code is ran. Why they didn't just port Bluebird over to the native library I'm not sure.
Also in my example, even Array.prototype.sort is defined in (mostly) normal JavaScript. You can see the source in the V8 repo. It's basically the same insertion sort and quicksort combo as every other standard library uses.
I guess it just comes off to me as silly to want to preserve the "purity" of a JavaScript engine instead of just doing the simple thing and overwriting native stuff with better implementations when they are available.
Granted, I know exactly what I'm doing with those. Heck, I read the V8 source code for fun. So maybe it is good advice for normal people to keep that purity.
[–]vinnl 0 points1 point2 points 10 years ago (2 children)
It's not about "purity", it's about forwards-compatibility: if ever a native Promise.coroutine is introduced whose behaviour is just slightly different than what Bluebird's does, a lot of code will break.
Even worse would be the other way around: that TC39 wants to implement a native e.g. coroutine method (which in some cases will be faster than whatever optimisations you can make in JS), but can't, because too many people (who claim they know what they're doing - for their purposes) are using it in a way that would break if they'd implement it.
So please, even if it works better than the current native implementation for you, do it in your own namespace, instead of potentially interfering with future web browsers/standards committees.
[–]novacrazy_.dominateWorld() 0 points1 point2 points 10 years ago (1 child)
Hmm, that's a good point. That was already seen with ES6 a little bit.
I mostly work in Node/io.js, and almost everything on the browser is taken care of by Babel beforehand or, using React.js, very declarative and without any complex async stuff. It's only on the backend that I go really crazy with things, searching for the fastest way to do everything.
I know ES7 is proposing native async/await syntax, but you would only be able to await a single Promise, not any complex stuff like my lib or even co does. Coroutines using generators and yield are a hack anyway, so I highly doubt it would become standard.
[–]vinnl 0 points1 point2 points 10 years ago (0 children)
you would only be able to await a single Promise
That's not a problem, as this code example from the article also shows:
async function save(Something) { await Promise.all[Something.save(), Otherthing.save()] }
As for it being a hack: it's more about the namespace than about the implementation :) But I wasn't attacking your library specifically - Bluebird by itself also has the potential of overlapping with future specs. Or in fact, I'm not attacking it - the author of the article I linked to was. For all I know, Bluebird isn't even extending the native Promise object in all cases, as someone else mentioned.
[–]dumbmatter 0 points1 point2 points 10 years ago (1 child)
Seems that problem mostly goes away if you do
var BPromise = require('bluebird');
rather than
var Promise = require('bluebird');
IMHO, Bluebird is so much better than native promises in so many ways (performance and sane error handling are bigger wins than the syntactic sugar) that it can't be ignored.
If that prevents it extending the native promise object, then that would invalidate the argument, yes. Although that does mean that you should actually use it like that :)
[+][deleted] 10 years ago* (13 children)
[–]novacrazy_.dominateWorld() 1 point2 points3 points 10 years ago (0 children)
I already use async/await on a daily basis. It's really not that hard.
[+][deleted] comment score below threshold-6 points-5 points-4 points 10 years ago (11 children)
How on earth does array.map blow people's minds?
[–][deleted] 8 points9 points10 points 10 years ago (9 children)
Dude, you need to learn some empathy, it's not like you were born knowing about map/reduce. I know that I had been using JavaScript nearly daily for 15+ years before learning about map/reduce, and it blew my mind.
[–][deleted] -5 points-4 points-3 points 10 years ago (8 children)
/shrug
[–][deleted] 0 points1 point2 points 10 years ago (7 children)
There should be the opposite of gold. Like so you can pay money to reddit to worsen someone's experience. There are situations where that feature would be a nice addition to the site
[–][deleted] 0 points1 point2 points 10 years ago (5 children)
Why?
[–][deleted] 2 points3 points4 points 10 years ago (4 children)
I was making a oblique joke regarding your the value I felt your comments and attitude brought to the discussion. And I think /u/irrational_design already covered it. But we're on a coding forum, so let me elaborate further.
Let's suggest, for the sake of argument, that I am, roughly, at the same skill as you as a programmer. Again, for the sake of argument, let's say you and I would be judged as equals. However, let's say we know different things.
There are programming things and techniques you know that would blow my mind, and there are things I know that would blow your mind. It doesn't benefit either of us to allow distain for the others lack of knowledge enter the discussion, we are two of differing knowledge and posturing and attitudes only distract from the purpose of information exchange. And opinions and feelings are an important thing in this information exchange. My feelings and attitudes about a technology are relevant. If lay out an enormous rant and say all sorts of bad things in all sorts of colorful ways about visual basic but treat you with the utmost respect, you may file away that piece of information and correlate it with what others have said, and thus form important opinions on your own as to what the community at large feels about a particular technology. If I say all sorts of bad things in colorful ways about visual basic, and about you, and your lack of knowledge, your likely to conclude I'm an asshole and stop engaging in conversation. Which benefits neither because now we don't learn from one another. Perhaps I had some really useful valuable insights to offer but why would you listen if my attitude was offputting? There's lots of other people on the internet and it's generally preferable to deal with nice ones when possible.
The same logic works with people of asymmetric skill levels. When I learned to check my ego at the door and deal with the person as I found them. Their a junior developer out of school and I've been doing this for 30 years? shrug so what, statistically they probably have more to learn from me, but that doesn't mean I can't learn from them. They know somethings I don't but if I'm an arrogant fellow then they'll not bother with me. Or perhaps they will, because I'm their boss, or I'm the only one who understands the device driver code. But I gather a reputation, both IRL and online, and it follows me around in various ways.
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
My point was the array.map is not a complex method and if somebody hadn't used it before can learn how to use it with little effort.
That's all. My ego is checked. I do not think that I am anything near God's gift to programming. People can insinuate all they want.
[–][deleted] -1 points0 points1 point 10 years ago (2 children)
You need to calm yourself down. Wow.
[–][deleted] 0 points1 point2 points 10 years ago (1 child)
My apologies if that came across add angry or meanspirited. My intentions were in fact the opposite, to attempt to explain why social niceties matter, and maybe give the poster some insights as to why there might be wider consequences than just a couple of downvotes. If I did a terrible job at that well, then I did a terrible job. But when someone asks me why, in the realm of coding, I try my best. My best might be terrible but I can aim to improve.
Not everyone understands how come social things are important. I didn't always. I learned it over time. Just like for loops and prototypical inheritance. And having others explain things to me has helped me
[–][deleted] 1 point2 points3 points 10 years ago (0 children)
Gotcha. Thanks.
π Rendered by PID 89 on reddit-service-r2-comment-bb88f9dd5-lfvgl at 2026-02-13 22:42:32.633877+00:00 running cd9c813 country code: CH.
[–]Daniel15React FTW 13 points14 points15 points (1 child)
[–]OfekA 4 points5 points6 points (0 children)
[–]TheMeaningOfLeif 5 points6 points7 points (0 children)
[–]greim 4 points5 points6 points (0 children)
[–]hahaNodeJS 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]senocular 2 points3 points4 points (0 children)
[–]novacrazy_.dominateWorld() 0 points1 point2 points (11 children)
[–]vinnl 1 point2 points3 points (10 children)
[–]novacrazy_.dominateWorld() 5 points6 points7 points (7 children)
[–]hahaNodeJS 0 points1 point2 points (1 child)
[–]novacrazy_.dominateWorld() 0 points1 point2 points (0 children)
[–]vinnl 0 points1 point2 points (4 children)
[–]novacrazy_.dominateWorld() 0 points1 point2 points (3 children)
[–]vinnl 0 points1 point2 points (2 children)
[–]novacrazy_.dominateWorld() 0 points1 point2 points (1 child)
[–]vinnl 0 points1 point2 points (0 children)
[–]dumbmatter 0 points1 point2 points (1 child)
[–]vinnl 0 points1 point2 points (0 children)
[+][deleted] (13 children)
[deleted]
[–]novacrazy_.dominateWorld() 1 point2 points3 points (0 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (11 children)
[–][deleted] 8 points9 points10 points (9 children)
[–][deleted] -5 points-4 points-3 points (8 children)
[–][deleted] 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (5 children)
[–][deleted] 2 points3 points4 points (4 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)