Handle Async Operations in Node.js - node.js

I have been using the async package from NPM inside my Node.js service to run different operations in sequence. Example: when I would like to run functionA, functionB, functionC in sequence. I would use Promise in Node.js, but this means using callbacks which are not that efficient when we have lot of functions which need to run in sequence.
I have recently familiarized myself with Reactive Programming using Observables and this is widely being used in frontend frameworks such as AngularJS and React. I was wondering if Node.js has something similar to that and if there are any better ways to handle async operations in sequence.

Related

How does Hystrix for NodeJS work despite being single threaded?

hystrixjs is an implementation of Hystrix for NodeJS.
But I'm not able to understand how it works in a single threaded js. How does it handle timing out the task?
The documentation talks about that a bit
Since this library targets nodejs application, it is by far not as
complex as the java implementation (remember... just one thread... no
concurrency, at least most of the time...). So how does it accomplish
the goals outlined above:
wraps all promise-based calls to external systems (or "dependencies") in a "Command"
Does that mean all the library is just run the promise and letting node handle the multi threading part(like network calls being handled by OS threads)?

Node is non blocking. Then why is async keyword used

Whenever i use await. I need to mark the function as async (non blocking). Isn't node by default async? What significance this keep words add.
Node is not by default async. Node favors asynchronous APIs because it is single-threaded. You may write synchronous code. It depends on APIs and modules you are using, not on node itself.
From nodejs.org blog https://nodejs.org/en/knowledge/getting-started/control-flow/how-to-write-asynchronous-code/
Node.js promotes an asynchronous coding style from the ground up, in
contrast to many of the most popular web frameworks.
So while node.js suggests async coding, it is still JavaScript, and Javascript does not do async unless you tell it to, through callbacks, promisify, or async/await.

node.js redis synchronous solution

I look for the package that allows to work with redis in synchronous manner. The problem is I don't want to rewrite the code for async flow.
The possible solution is
Find synchronous library to work with redis.
Implement a middle layer script in some synchronous language and communicate with this script by synchronous execution operation.
I tried to use synchronize with no success so far.
You may want to give a try at co, which will let you write non-blocking code using almost any existing node library. Here is a specific example using node-redis.
Please note that co requires node 0.11.x (with --harmony flag) or 0.10.x with a wrapper like gnode.

Making asynchronous (node.js) in Meteor

Some questions about Meteor and node.js:
Meteor is based on node.js, but can we do node.js IN meteor? (or use it with meteor?)
I would like to combine the power of two and make this (for exemple): http://www.youtube.com/watch?v=EqWD1WGrdjw, in/coupled with meteor...
Is this possible?
I'm assuming you want to do async stuff. You need to use Fibers (node-fibers) inside Meteor. There are great screencasts from cmather explaining them:
http://www.eventedmind.com/posts/nodejs-introducing-fibers
http://www.eventedmind.com/posts/nodejs-using-futures

Async access to MongoDB using Aleph/Lamina

I have been reading about Clojure for some time and I'm considering it as a replacement to Node.js (which I have used for another project). The most promising library seems to be Aleph/Lamina, which unfortunately doesn't have nearly as many examples as Node. My questions are:
How can I process requests with a chain of async operations, such as reading a document from MongoDB, doing some calculations, saving the new document and sending it in the response? I was not able to write it from the examples in Lamina wiki page. It sounds like a pretty common use case and I was surprised not to found any code showing it. It would be great if you could show me some sample code.
Is this setup adequate for a heavy-load server (say, tens of thousands requests per second)? I can't afford to create one thread for each new request, so I need something similar to the Node approach.
Is there any example of a medium- or large-sized company out there using any of this?
Is there any better Clojure replacement to Node (besides Aleph/Lamina)? Perhaps Clojurescript targetting Node? My client is not written in Javascript, so using the same language in both client and server is not an advantage in my case.
Thanks!
Few pointers:
You need to look at Aleph which builds HTTP abstractions over Lamina channels abstraction.
Reading and writing docs to MongoDB can be async but the library should provide this. In Node.js the MongoDB library has to be async other wise it would screw up the Node programming model, where as this is not the case with Clojure so most probably the Clojure MongoDB library provides non-async function.
Async operations are only helpful in case of IO i.e reading/writing to mongodb, sending response back etc. Generation computations are CPU bound operations and has nothing to do with async model.
Vert.x is Java world Node.js. Clojure support is on roadmap. I would prefer Aleph as you can play in async and non-async world as required.

Resources