Nodejs chaining with async

method-chainingnode.js

I'm trying to chain a series of methods that are async. I have heard of promises and futures but what I'm looking for is:

obj.setup()
    .do_something()
    .do_another_thing()
    .end()

and not:

obj.setup()
    .then(...)
    .then(....)

I have come across a tutorial that explains how to do this, but unfortunately the penny hasn't dropped: http://www.dustindiaz.com/async-method-queues/ So I'm looking for a module. This module, https://github.com/FuturesJS/FuturesJS , seems to have chainify in its API but there's no documentation on how to use it. In fact I can't find any documentation on how to use modules to achieve what I'm looking for, but plenty on using promises to get then().then().then() which is not what I need.

Currently my module looks like:

var obj = function(){
   this.async_method = function(){}
   this.async_method_thingy = function(){}
   this.async_method_foo = function(){}
}
var o = new obj()

var _ = {
   "setup" : function(){
      ...
      return this
   },
   "do_something" : function(){
      o.async_method()
      return this
   },
   "do_another_thing" : function(){
      o.async_method_thingy()
      return this
   },
   "end" : function(){
      o.async_method_foo()
      return this
   }
}

module.exports = _

any help is appreciated 😉

Best Answer

I think you are looking for the async module on npm.

Find it on github at https://github.com/caolan/async.

This module supports a lot of utilities for doing things asynchronously that are typically synchronous, like a linear search through an array.

More importantly for you, it allows for chaining asynchronous functions. It has 2 modes you can use: parallel or serial. In parallel mode, it runs the async functions in parallel (this is a lie as nothing in parallel in node.js, but it will simply context switch based on I/O calls as any async stuff works in node). In serial mode, it runs each function after the previous one finishes by chaining the callbacks together.

From the quick examples on their README:

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);

async.series([
    function(){ ... },
    function(){ ... }
]);

So if I understand your question correctly, what you want to do is this:

async.series([
  obj.setup.bind(obj),
  obj.do_something.bind(obj),
  obj.do_another_thing.bind(obj),
  obj.end.bind(obj)
]);

This is how under the hood async will handle this and you could do it yourself if you don't want to use the module:

obj.setup(function () {
  obj.do_something(function () {
    obj.do_another_thing(function () {
      obj.end();
    });
  });
});

As you can see, this gets messy as you add more and more functions to the chain. That is why the async.series call is more maintainable.

You also need to update your functions to use callbacks. No matter what tools or frameworks you use, if your async functions don't pass a callback, there is no way to know whey they finished and therefore no way to move to the next function in the chain.

Function changes:

var _ = {
   "setup" : function(callback){
      ...
      return this
   },
   "do_something" : function(callback){
      o.async_method(callback)
      return this
   },
   "do_another_thing" : function(callback){
      o.async_method_thingy(callback)
      return this
   },
   "end" : function(callback){
      o.async_method_foo(callback)
      return this
   }
}
Related Topic