Search code examples
javascriptjquerypromisedom-eventsjquery-deferred

What is the shorthand for setting up dependencies in jQuery deferred objects?


Do jQuery deferred objects have any shorthand to set up dependencies, i.e. "when I'm done, that thing is done as well?" The below feels cumbersome:

a = new jQuery.Deferred();
b = new jQuery.Deferred();

a.done(function(result){alert(result);});
b.done(function(result){a.resolve(result);}); // this line doesn't feel right

b.resolve("Hey there!")

I'd love something like b.alsoResolves(a). I've looked at the documentation, but can't seem to find anything appropriate. Is there any better way to created dependencies between deferred objects than the one above?


Solution

  • Since we are able to register multiple callbacks sequentially now, I personally find it cleaner to set dependencies this way:

    a = $.Deferred();
    b = $.Deferred();
    
    a.done(fncDoStuffForA);
    
    b.done(fncDoStuffForB);
    b.done(a.resolve)
    
    b.resolve("Hey there!")
    

    http://jsfiddle.net/rkw79/cdkG3/