开发者

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

开发者 https://www.devze.com 2023-03-23 13:30 出处:网络
Do jQuery deferred objects have any shorthand to set up dependencies, i.e. "when I\'m done, that thing is done as well?"The belo开发者_运维问答w feels cumbersome:

Do jQuery deferred objects have any shorthand to set up dependencies, i.e. "when I'm done, that thing is done as well?" The belo开发者_运维问答w 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?


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消