RK* - rikkertkoppes.com

thoughts

Asynchronous method chainer

Ever encountered having a bunch of methods doing asynchronous things that you would really like to chain? I have written a little chainer utility to do just that. Instead of giving callback parameters to your methods, you can now chain them as if they were synchronous.

Imagine you have some methods that take a callback function to execute when they are done. You end up nesting these. Think about something like:

a.doSomeHTTPRequest(foo,bar,function() {
  a.openDialogWindow(some,parameters,function() {
    a.acknowledge();
  });
});

Or, imagine chained animations:

a.moveRight(200,function() {
    a.moveDown(200,function() {
      a.moveLeft(200,function() {
        a.moveUp(200);
      });
    });
  });

Wouldn't it be nice if you could just do:

a.moveRight(200)
 .moveDown(200)
 .moveLeft(200)
 .moveUp(200);

Let's make that more concrete. Suppose a is an instance of a class Animator with the following signature:

Animator.prototype.moveLeft = function(d,callback)
Animator.prototype.moveRight = function(d,callback,bar)
Animator.prototype.moveUp = function(d,callback)
Animator.prototype.moveDown = function(d,foo,bar,callback)

Note the callback parameter occurs at different locations for illustration.

I can now make a Chainer instance for the Animator instance (a).

var c = new com.rikkertkoppes.util.Chainer(a,{
    methods: [
      'moveUp','moveDown','moveLeft',
      {name:'moveRight', index: 1}
    ]
  });

The constructor takes 2 parameters, the instance to chain and some configuration properties. The methods property is an array listing the methods that you would like to be able to chain. It can be an array of strings or an array of object literals. In the latter case, a name key should at least be present indicating the method name. Optionally, you can indicate which parameter of the method is the callback parameter. By default, the chainer assumes it is the last parameter.

Furthermore, a scope parameter can be added to adjust the scope in which the chain will be executed. By default it is the scope of the instance given.

The chainer is now set up and we are ready to do what we actually wanted to do:

c._chain()
 .moveRight(200)
 .moveDown(200)
 .moveLeft(200)
 .moveUp(200)
 ._do();

Note the _chain and _do methods at the start and end. The latter method indicates that the chain should be executed immediately. Alternatively, one can call the _do method at a later stage. Moreover, one can just omit ._do() and just call the returning chain directly:

c._chain()
 .moveRight(200)
 .moveDown(200)
 .moveLeft(200)
 .moveUp(200)();

Get it

All this code is public domain.

Additional resources (top 15)

Below is a list of additional resources that might contain extra information about the subject at hand. These are all sites linking to this one (i.e. backtracking).

  1. +javascript +asynchronous +scope +chains - Google Search (1)

comment

Black Jack gratis (2010-02-19)

I want to know that how to chain asynchronous calls when a Web service method makes more than one asynchronous call and the calls must execute sequentiallyThat intermediate callback then asynchronously calls to get the royalties for that author, if the author is valid.I do not know about it.So please tell me..about this.

Ring Tones (2010-05-13)

Method chaining is sometimes preferable when setting properties on an object. This program generates a class that extends a target class and adds methods that call property setters and return "this" to support method chaining.............................................. http://www.mobtech.in

Best online craps (2010-06-10)

the course of developing NoteTag, we tried dozens of solutions to “the asynchronous problem”. The bad news: there is no one-size-fits-all solution. The good news: there are a few key rules-of-thumb which can guide you towards choosing the solution that’s right for the situation.

SEO Expert (2010-06-10)

The basic pattern is to call one of the asynchronous methods such as downloadFileAsync(). When an asynchronous method is invoked a file-transfer job is queued and the method returns. The job will continue running in the background until it has completed.

Bonds (2010-06-10)

He said for each day that I do my task of writing, I get to put a big red X over that day. "After a few days you'll have a chain. Just keep at it and the chain will grow longer every day. You'll like seeing that chain, especially when you get a few weeks under your belt. Your only job next is to not break the chain.

Online Mobile Store (2010-06-10)

the sake of an easy life, most of the time we want to write “synchronous code” but asynchronous is the only way to avoid some rather nasty usability issues. This means rather than being able to write simple code, as we’d like to, such as;

Interior Home Decoration (2010-06-10)

The paper explores database replication in the electronic commerce. We present new asynchronous replication method of the database, which is accessed from outside the company. The computer configuration of the company is composed of the one primary copy and several secondary copies.

Add comment
older articles

AdministrationAtom feed