Does ES6 promises replace the need for async.js methods? - node.js

With the introduction of http://es6-features.org/#PromiseUsage in ES6, is https://github.com/caolan/async still relevant? Do async.waterfall and async.series provide any benefit over promises?

Yes and no. The async functions allow to just have it prepared for you rather then you building it yourself and the async still work everywhere. ES6 promises are pretty great. They allow to do a lot and when paired with the proposed ES7 async/await they might remove need for the package all together depending on how its implemented in the future.

Related

Async vs Deasync in reality

The background
I'm writing my next server project in node. The previous version was written in language D, which is majorly an synchronized implementation. We all know, that JS uses a lot of async This is good for performance, but hard to maintain (at least for me). Because node and its node modules might use different async approach (callback, promise and await/async), which makes the code looks not in a consistent style. I'm looking for a balanced approach, that not only can create a high quality and performance node application, but fit the common programming style.
The problem
After first round prototyping, I have my first server. When I start the server, it will first read configuration and then load a set of modules. I expect them to be loaded sequentially. But as I mentioned above, node modules might work in async way. I expect module b to be loaded exactly when module a is loaded, I must use Promise pattern. I have to write code in crazy way, although Promise is already a very programmer friendly approach.
No, I go to the other direction. Currently I choose deasync, which is a gyp module for node. It can really blocks the code execution like this.
const deasync = require('deasync');
let done = false;
loadModule1Async((err, result) => {
done = true;
});
deasync.loopWhile(() => !done);
done = false;
loadModule2Async((err, result) => {
done = true;
});
deasync.loopWhile(() => !done);
OR even more magical like this
const deasync = require('deasync');
const loadModule1Sync = deasync(loadModule1Async);
const loadModule2Sync = deasync(loadModule2Async);
try {
loadModule1Sync();
loadModule2Sync();
} catch(ex) {
console.error(ex);
}
IMO, the deasync way is more straightforward to me, but I have two concerns:
It need to be compiled against the target platform, so that the deployment process will be complicated for production.
I checked its issue list. There are some known serious issue, e.g. hanging in some extream cases.
The question
Can the coming new feature async/await really do the same thing as deasync does? If it is never gonna happen due to the JS nature. I might stop thinking about this.
Please share me some lights on the way to node.
I don't know too much about how deasync works, I took a look at it and looks very convenient, but I don't think it's thought to be used through all your code in a server.
little bit performance downgrade
It depends on your case, but it could be more than a little downgrade. You are wrapping all your asynchronous methods in your code, that does not sound good in general. Doing a benchmark could be a good idea.
Other thing:
Because node and its node modules might use different async approach (callback, promise and await/async)
In general modules follow callback style, Promise style, or both of them. Most of the big ones are compatibles with both of them (in general if you don't pass a callback a Promise is returned).
async/await is different, this works on top of Promises, so a module method that returns a Promise can be used with async/await in your code.
So answering your question, I would stick with Promises in the whole code, it's the tendency now, and in general they are easier to deal with, compared to callbacks.
To me async/await is the easiest way, since it keeps calls asynchronous, but the code is written in a synchronous way. Sadly at the moment async/await is only usable in Node.js through a flag, so it's not recommended to be used in production.

Why it use Q,bluebird framework when using promise? [duplicate]

This question already has an answer here:
Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises? [closed]
(1 answer)
Closed 6 years ago.
During studying promise in node.js. i have some doubt.
'promise' is already defined in node.js
but usally it seems to use additional promise framework like Q,bluebird,RSVP etc.
Is there any reason?
Is the reason that core node.js function cannot support promise with promise.denodeify function?
I can't say for Q, but Bluebird is a lot faster than native Promises, and provides a bunch of extra features on top of the native promise.
It's the same reason people use lodash despite having [].map() for years now.
Additionally, Bluebird has better browser support than the native Promise implementation.
You normally only use Bluebird on the server, though. The extra features and speed are not worth the extra size of the library the user has to download.
Well, promise is the native JavaScript object. All those libraries are userland implementations. For example if we look at bluebird it has:
A lot of utility functions and helpers that make your life easier.
It has typed .catch which makes sure you don't catch programmer errors by mistake.
It has .some .any .map .filter and so on for working with collections easily.
It has .reflect and synchronous inspection of promises.
It doesn't swallow errors by default when throwing in a then handler.
Built in coroutine (generator) support for flattening asynchronous flow.
It's faster (usually between 4x and 10x) than native promises in different browsers.
It provides more debugging hooks and better stack traces.
It provides warnings against common promise anti-patterns.
It lets you override the scheduler so you can determine how it schedules tasks.
It supports promise cancellation with sound semantics which is proposed for native promises but not yet adopted.
So in a tl;dr; :
It's faster.
It's more debuggable.
It has a richer API.
Now, whether or not you should use it is up to you - there is always overhead in including libraries - I'm biased as a core contributor.

Is iced coffeescript supporting or going to support promises?

As a node.js practitioner, I am actively using coffeescript and functions based on promises. Recently I have found out iced coffeescript, and I wonder whether my approach can go along with "iced await defer" one.
So here is my question, is there any support for the promises implemented or planned for the iced coffeescript? Or would rather iced coffeescript made me come back to the callback functions world?
Also as a side question, as far as my knowledge goes, there is an ECMAscript 7 draft to introduce await async patten to solve the issue. Is iced coffeescript team planning to blend on it if it will become standard?
Cheers.
Iced is certainly active and in many cases (like parallel calls) is more flexible than promises. There is now an open PR for ES7 async at maxtaco/coffee-script#147, and is far more simple than the open PRs on regular Coffee (#3757, #3813).
The easiest way to use Iced with Promises is to nodeify the Promise. I use many Promise-based libs in my Iced code without trouble.
I think most coders using Iced just far prefer the clean syntax of await/defer over promises. A major advantage of using Iced now, is that the Iced code that works now will also work in the future, despite whatever underlying changes happen in ES6/7 and Coffee implementations as syntax is finalized.
Iced CoffeeScript development has stagnated lately. I regard ICS as interesting experiment - given its current adoption I would not really recommend using it. As for support or planned support there is none - there might be in the future but the issue tracker shows no issues discussing it have been opened.
ECMAScript 7 indeed has async functions - since those use promises there is no reason to assume any work is done in ICS to support them. Again - no one can tell the future and there might be work on those at a future point in time.

Bluebird instead of Co in Koa?

Seems like Bluebird overlaps Co in generator/coroutine related functionality. Bluebird is espoused to have exceptional speed-performance, so for discussion sake, (assuming the aforementioned overlap premise is true) if one wanted to substitute Bluebird for Co in Koa (Node.js context), could it be easily be done without diminishing Koa's functionality, and if so how?
(My guess is it can't practically be done since it seems Koa is built over Co and doesn't explicitly expose it, but facades it. Such a substitution it seems would be tantamount to replacing jQuery with something else in Bootstrap)
First of all, bluebird and co are not comparable like that. You mean Bluebird.coroutine vs co (short for coroutine).
Now, the difference between Bluebird.coroutine and co is that co only allows you to yield a certain set of hard-coded types. While Bluebird.coroutine can be configured to support yielding arbitrary types, the documentation for example contains examples how you can add support for yielding thunks and callbacks.
Async generators are so trivial that the only differences there can be between implementations is what types you can yield and how it performs. Not much room to be better or worse.
However bluebird.coroutine is only a fraction of bluebird features.
Generators only solve the problem of making a sequence of actions less verbose. There is a lot of useful functionality for more advanced needs like resource management, concurrency coordination, error handling, cancellation+timeouts and long stack traces which are impossible or extremely painful if you only have async generators powered by thunks/callbacks/minimal promises.
You can make a drop-in replacement for co by configuring all the yield types that co supports and then just using bluebird.coroutine:
var co = require("bluebird").coroutine;
// Configure all yield types you need using co.addYieldHandler
// See documentation for examples
module.exports = co;
However this doesn't really make any sense since very little code actually should run directly in your request handler - the functions that the request handler calls however do. And those functions are not helped by koa (hmm so what is the point of koa again? :D), so they can be bluebird coroutines directly.
esailija said this about Bluebird,
a feature is being added that allows not only yielding callbacks, thunks etc but any arbitrary thing that comes to your mind. Bluebird is also the fastest. So after this version koa should be just using bluebird indeed. See https://github.com/petkaantonov/bluebird/issues/131#issuecomment-36975495
That said, I don't believe him. And, I don't believe a bluebird wrapper would be faster than Co -- if such a thing were possible. Co.js works, and there is no way possible to get Bluebird.js to pass the tests currently. If you're using ES6, ignore Bluebird entirely and use Co.

How to avoid deep nested code with mongoose / node?

I am trying to improve the readability of a biggish nodejs app that uses a lot of mongooose. The problem is that with a lot of dependent queries the callbacks get out of hand.
What are the practices of handling this issue?
There are three common solutions for your problem.
First one is async.js lib.
Second one is using Promises. There are more then one implementation of promises in node.js. I know three implementations:
node promise
vow
Q promise
Third one is to use Fibers. There is fibers promise library that do all the tricky work for you.
There was plenty of similar questions before. For example, check this one.
All this libraries do the same thing - they make node.js asynchronous code pretty and readable. So, just choose one that looks simpler for you.
As for me, I prefer async.js lib.
Update: mongoose.js have its own build-in promise - mpromise. You can access it as mongoose.promise. But whenever you call exec() function on a query in mongoose it returns you a promise. I never actually used mongoose.js promises except for the REPL, but you may give it a try.

Resources