Is there a way to cancel a deferred callback queue in progress? I have an arbitrary amount of ajax calls. I'd like to stop further ajax requests when data of success returns specific marker:
this.oDeferred=$.Deferred();
this.oChain=this.oDeferred;
for(var i=0; i<this.aKey.length; i++) {
(function(iKey,self) {
self.oChain=self.oChain.then(function(){
return $.ajax({
url:self.aUrl[iKey],
type:'post',
data:{'ajax':true},
dataType:'json',
success:function(data) {
if(data.bCancel==true) {
//stop deferred object here!
}
}
});
})
}(this.aKey[i],this))
}
this.oDeferred.done(function() {
console.log('done')
});
this.oDeferred.resolve()
By the way - the function done() is fired autonomous after all ajax requests are made. How to execute a function after all ajax requests are done?
Thank you in advance!
Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.
promise() will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists. If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point.
The defer attribute is a boolean attribute. If the defer attribute is set, it specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing. Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).
Deferred method can be passed an optional function, which is called just before the method returns and is passed the new deferred object as both the this object and as the first argument to the function. The called function can attach callbacks using deferred. then() , for example.
The answer is yes. Two approaches are possible.
The .then()
method returns a new promise, the state of which is determined by what is returned from the handler(s) passed to the method.
Thus, Deferred/promise callback queue can be effectively canceled by returning from a .then()
handler a promise that is never resolved and never rejected. Such a promise can be made from the .then()
handler's done handler (first argument) or its fail handler (second argument). The same cannot be achieved with .done()
, .fail()
or .always()
methods, which return the original Deferred/promise without modification.
An uncaught error thrown from within a .then()
, .done()
, .fail()
, .always()
or .progress()
handler will kill a method chain by killing the event thread in which it is running.
An error can be thrown deliberately with eg throw('deliberate error')
.
It should be noted that both approaches will only suppress chained method handlers (or the equivalent achieved by assignment).
With either approach, any asynchronous process that is already initiated at the point the suppressing return/error expression executes will continue and any corresponding done/fail/always/progress handler already in place may fire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With