Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chained jQuery promises with abort

Tags:

I'm currently writing API code which, several layers deep, wraps $.ajax() calls.

One requirement is that the user must be able to cancel any request (if it's taking too long, for example).

Normally this is accomplished via something simple, like:

var jqXHR = $.ajax(..);
$(mycancelitem).click(function () {
     jqXHR.abort();
});

However my code looks more like this:

function myapicall() {

    var jqxhr = $.ajax(…);
    var prms = def.then(function (result) {
        // modify the result here
        return result + 5;
    });

    return prms;
}

The problem here is someone calling myapicall() only gets a jQuery.Promise with no way to abort it. And while the sample above is very simple, in my actual code there are several layers of chaining, in many places.

Is there a solution to this?