javascript - How to make a jQuery Deferred object resolve/reject with the same 'resolved/rejected' state of another deferred? -
i'm writing several functions deferred objects depend on varying combinations of other deferred objects.
function takesonesecond() { return $.deferred(function(deferred) { // something... }).promise(); } function takesoneminute() { return $.deferred(function(deferred) { // something... }).promise(); } function takesthreeminutes() { return $.deferred(function(deferred) { // something... }).promise(); } function myswitchingfunction() { return $.deferred(function(deferred) { // here.. // chooses 1 of several other functions call. if(/* choose 1 second */) { // tie ourselves '1 second' function. // call function. takesonesecond().done(function() { deferred.resolve(); // if that's done, i'm done too. }).fail(function() { deferred.reject(); // if failed, i've failed too. }); } else if(/* choose 1 minute */) { // etc.. } else if(/* choose 3 minutes */) { // etc.. } }).promise(); }
i'm writing snippet of code lot, there no other way make deferred mirror or cascade same 'resolved' or 'rejected' state of deferred?
takesonesecond().done(function() { deferred.resolve(); // if that's done, i'm done too. }).fail(function() { deferred.reject(); // if failed, i've failed too. });
i think don't need construct new promise @ all. return first promise.
function mysecondfunction() { // here.. // chooses 1 of several other functions call. // in case, assume i've chosen 'myfirstfunction' function. // call function , return promise return myfirstfunction(); }
if want emphasize "at same time" part maybe resolve different value, create new 1 chaining .then
:
function mysecondfunction() { return myfirstfunction().then(function(resultoffirst) { // ignore , return differentresult; }); // errors propagate automatically }
Comments
Post a Comment