Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Favor promises over async #4493

Closed
Radiergummi opened this issue Apr 3, 2016 · 9 comments
Closed

Favor promises over async #4493

Radiergummi opened this issue Apr 3, 2016 · 9 comments
Labels

Comments

@Radiergummi
Copy link

I think it would be great to get rid of the async module and replace these calls with promises. It's faster, cleaner, easier to wrap your head around and will becoming more and more important in the next years. What do you think?

@MegaGM
Copy link
Contributor

MegaGM commented Apr 3, 2016

I think so, but how much refactoring will it take? Tons

@Radiergummi
Copy link
Author

Never let developer convenience decide over product quality was one of the first things i was taught 😋

@julianlam
Copy link
Member

I've attended a couple of presentations about promises, and I have not heard a sufficient argument to switching from our use of async. Don't take this sentence to mean that I am against it, I am simply attempting to view the situation objectively.

Every discussion or presentation I attend focuses on how promises are a cure for the callback pyramid, and are faster/easier to read compared to using callbacks, but I have not seen concrete evidence that promises are better than async. If anything, they're equivalent ways of doing the same thing. Keep in mind I have very limited knowledge of what promises (either via Q or bluebird or what-have-you) can do:

  • Can promises do a waterfall style with each method passing its result onto the next?
  • Can promises do a series style? each? eachLimit? map? mapLimit? whilst? parallel?
  • Are there benchmarks showing they are faster?

More importantly, is there something significantly broken with async that would force us off of it?

I understand that promises are built into ES6, and I have no problem transitioning in time, but at current, as with when we started, I do not see a compelling enough reason to switch.

(That, and a ton of plugins require async from core, so that's got to be handled too)

@julianlam
Copy link
Member

I specifically did not comment on "developer friendliness" because after nearly three years of using async, I am very accustomed to it and find writing async to be very natural.

However I do realise this is not the case for all developers (esp. new developers/contributors), and if the prevailing winds suggest a shift to promises, then that's the way we will sway.

Again, it's a matter of when, not if.

@Radiergummi
Copy link
Author

@julianlam I found this to be an interesting read which covers most of these points. What I think is a great argument for using promises is that the probability of them throwing uncaught exceptions is way lower.
Having read quite a bit complex promise code, I found it to be almost-too-easy written, almost language syntax like. Just take a look at the fetch API for example which will replace XMLHttpRequests...

@julianlam
Copy link
Member

Thank you for sharing, good bedtime reading 😄

@ghost
Copy link

ghost commented Apr 4, 2016

In addition to the article:

  • mapLimit is easier to project on up-to-date bluebird than the article shows since bluebird now supports {concurrency: int=Infinity} as third parameter for map and filter.
  • whilst is a tricky one; I solve those use-cases with a named kind-of-recursive (no real recursion, so no problems) function:
/* var items = [1,2,3,4]; */
var i = 0;
function iteratee() {
  if (!/* whilst check condition */) { return; }
  return doSomething(items[i++])
    .then(iteratee); // .then(function () { if (/* doWhilst check condition */) return iteratee(); });
}
return Promise
  .attempt(iteratee)
  .then(function () { /* done */ }, function (err) { /* error */ );

(untested)

All in all my current setup is lodash plus a promise-library for nearly every project of mine. Speaking of, you guys could switch to lodash in favor of underscore[.deep] as well 😉 Maybe I'll make a PR for this some time...

@psychobunny
Copy link
Contributor

Don't have a big opinion about async vs promises, but both can be messy if you don't do it right. We have had some ridiculously messy async control code in the past.

Leave this to our community forum for discussion please. I'm closing because it's a) not an issue per se b) who can fix this in one commit?

@lo1tuma
Copy link
Contributor

lo1tuma commented Aug 16, 2016

I would like to suggest to re-consider switching to promises.
In my opinion the biggest advantage of using promises is error-handling. Promises (especially bluebird) makes it much harder to forget about handling asynchronous errors (see also: #4951). It also reduces the amount of duplicated code tremendously because errors are automatically propagated, so you don’t have to write

if (err) {
    return callback(err);
}

all the time.

Apart from that bluebird has an excellent debug mode which gives you long stacktraces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants