Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery deferred: cancel progress

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!

like image 827
campari Avatar asked May 02 '13 10:05

campari


People also ask

What is Deferred in JQuery?

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.

How use JQuery Deferred and promise?

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.

How do I use Javascript Deferred?

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).

How do you use Deferred?

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.


1 Answers

The answer is yes. Two approaches are possible.

.then()

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.

  • by returning a value/object that is a non-promise, a new promise is passed down the method chain with the same resolved/rejected status as the original promise but resolved/rejected with the returned value/object.
  • by returning a promise, that promise is passed down the method chain, with a status that is independent of the original promise.

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.

Throw an error

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').

Note

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.

like image 149
Beetroot-Beetroot Avatar answered Sep 22 '22 03:09

Beetroot-Beetroot