javascript - Advantages/Disadvantages of Passing a Function into Deferred's Constructor -
in playing deferred's see many different combinations of use. same, see ones differ slightly.
for example...
normally, see this:
here merely using deferred.
// see this... function dosomething(){ var deferred = $.deferred(); var myclass = new myclass(); myclass.dosomething(function () { deferred.resolve(); }); return deferred.promise(); };
occassionally, see this:
here passing function deferred's constructor...then using it.
// see this... function dosomething() { var deferred = $.deferred(function (deferred) { //q: there advantage doing instead? //q: mis-use? var myclass = new myclass(); myclass.dosomething(function () { deferred.resolve(); }); }); return deferred.promise(); };
my question is:
- is there advantage doing 2nd 1 instead?
- is mis-use of constructor?
- does practice create issue haven't seen yet?
i have yet see issue's arise method 2. so, i'm looking real insight here.
is there advantage doing 2nd 1 instead?
no, unless enjoy callbacks defeats purpose of promises.
is mis-use of constructor?
nope. jquery deffered doc
jquery.deferred( [beforestart ] )
beforestart type: function( deferred deferred ) function called before constructor returns.
does practice create issue haven't seen yet?
no doesn't, unless intended use myclass
somewhere else won't possible since defined within callback.
conclusion:
in end, it's more of personal preference. solution 1 seems more clean honest.
Comments
Post a Comment