Deferreds

by Dylan SchiemannJanuary 30th, 2008

A quick review of the DojoX Cometd JavaScript client shows that the return value of init, subscribe and other methods is a dojo.Deferred object. So what is a Deferred and what makes it so powerful and flexible in a Comet client library?

A Deferred provides a standard way of doing asynchronous / non-blocking programming. Originally implemented in Twisted Python, it was first ported to JavaScript by the MochiKit developers, and subsequently ported to Dojo. A Deferred is simply a promise that a function will, at some point, have a result. This is convenient because developers can attach callback functions and error callbacks in a chain to process results. The results of one callback function are passed to the next callback function in the chain.

For example, inside the DojoX Cometd chat client, we see:

this.subscribe = function(channel, objOrFunc, funcName){
  ...

  var _ds = this._deferredSubscribes;
    _ds[channel] = new dojo.Deferred();

    …
    return this._deferredSubscribes[channel];
}

…

this._deliver = function(message){
…
  switch(message.channel){
  …
    case “/meta/subscribe”:
      deferred = this._deferredSubscribes
        [message.subscription];
      if(!message.successful){
        if(deferred){
          deferred.errback(
            new Error(message.error));
        }
        return;
      }
      dojox.cometd.subscribed(message.subscription, message);
      if(deferred){
        deferred.callback(true);
      }
      break;
…
}

As you can see, Deferreds provide a very concise mechanism for dealing with asynchronous message handling. DeferredList takes this a step further and allows you to synch multiple Deferreds:

dojo.DeferredList([deferred1, deferred2]).addCallback(bothCompleted);

deferred1 and deferred2 need not both be requests. For example, one could be an animation or drawing routine that is to finish at the same time as a request.

Some of the more advanced auto-wiring of chaining Deferreds can be very convenient. For example, adding a Deferred as a callback method for another Deferred will automatically trigger the added Deferred's callback method().wired up.

The full source for Dojo.Deferred is well-documented and explains in great detail how to get the most out of this powerful asynchronous programming pattern, making it very suitable for Comet and Ajax client development, as well as pubsub topic systems.

3 Responses to “Deferreds”

  1. David Golightly Says:

    So how is this fundamentally different from the Subscriber pattern (http://www.dustindiaz.com/javascript-publisher/ or http://peter.michaux.ca/article/32)? It seems like lots of JS developers have been using similar patterns to create “custom events” or “publishers” for some time now… Of course, it’s still nice to propagate awareness of the technique…

  2. DylanSchiemann Says:

    @David: PubSub and the Observer pattern are synonyms, and have been around for quite some time (since the late 1990s I think). A Deferred is sort of a lower-level way of implementing pubsub, but can also be used to better manage XHRs, etc. (it is how Dojo’s XHR works in fact). Semantically the patterns are a bit different in the flexibility and approach they offer for async event handling.

  3. SitePen Blog » Pluggable Web Services with SMD Says:

    [...] simply call the methods on the object. Note that that when these methods are called they return a dojo.Deferred object so that the service can be executed asynchronously. Here is an example of calling a service [...]


Copyright 2015 Comet Daily, LLC. All Rights Reserved