5

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 ;)

1
  • to get an idea of where I'm stuck with the link in the question, here's a pastebin of what I'm trying with it: pastebin.com/FpMV33E8
    – Daithí
    Commented Jan 7, 2014 at 10:18

1 Answer 1

4

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
   }
}
2
  • I've gotten that far. The issue is using promises (i think) so that the end user only has to do: module.setup().do_this().do_that(), or if they like module.setup().do_that().do_this() instead of async.series([ obj.setup.bind(obj), obj.do_something.bind(obj), obj.do_another_thing.bind(obj), obj.end.bind(obj) ]); Think that maybe the only solution is to have my front facing methods build up an array, then have user run .run() as a last call in the chain, which would then run the promises/futures.
    – Daithí
    Commented Jan 7, 2014 at 14:21
  • My problem in async is that i cant run the first function with arguments.
    – ji-ruh
    Commented Jan 20, 2017 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.