Is there any analog to a 'finally' in jQuery AJAX calls?

JavascriptJqueryAjaxPromise

Javascript Problem Overview


Is there a Java 'finally' analogue in jQuery AJAX calls? I have this code here. In my always I throw an exception, however I ALWAYS want it to go to the then() method.

    call.xmlHttpReq = $.ajax({
        url : url,
        dataType : 'json',
        type : 'GET'
    }).always(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {

       throw "something";

    }).then(function() {

        alert("i want to always run no matter what");
    });

I have tried to use done(), complete(), and the another always(), but nothing seems to work.

Here is JSFiddle :

http://jsfiddle.net/qv3t3L0m/

Javascript Solutions


Solution 1 - Javascript

See this example:

$.ajax({
        type: "GET",
        dataType: dataType,
        contentType: contentType,
        async: TRUE,
        url: $('html form:nth-child(1)').attr('action') + "?" $('html form:nth-child(1)').serialize(),
        success: function(data) {
            console.log("FUNFOU!");
        },
        error: function(data) {
            console.log("NÃO FUNFOU!");
        },
        complete: function(data) {
            console.log("SEMPRE FUNFA!"); 
            //A function to be called when the request finishes 
            // (after success and error callbacks are executed). 
        }
    });

> For more informations: http://api.jquery.com/jquery.ajax/

Solution 2 - Javascript

.always() should work. See the The jqXHR Object section at http://api.jquery.com/jQuery.ajax/.

> jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { }); > An alternative construct to the complete callback option, the > .always() method replaces the deprecated .complete() method. > > In response to a successful request, the function's arguments are the > same as those of .done(): data, textStatus, and the jqXHR object. For > failed requests the arguments are the same as those of .fail(): the > jqXHR object, textStatus, and errorThrown. Refer to deferred.always() > for implementation details.

See also http://api.jquery.com/deferred.always/

Solution 3 - Javascript

The below suggestions will not work in jQuery, because jQuery's promise implementation does not handle errors thrown in methods passed to then. I am only leaving them here as an illustration of what could be possible if jQuery was promises/A+ compliant. As Bergi rightly points out, you will have to manually wrap your code in your own try catch block.

call.xmlHttpReq = $.ajax({
    url : url,
    dataType : 'json',
    type : 'GET'
}).then(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {

   throw "something";

}).always(function() {

    alert("i want to always run no matter what");
});

Although I'm not sure if jquery's promise supports always, an alternative would be to use then (again) and pass the same function as both successHandler and errorHandler, like this :

call.xmlHttpReq = $.ajax({
    url : url,
    dataType : 'json',
    type : 'GET'
}).then(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {

   throw "something";

}).then(function() {

    alert("i want to always run no matter what");
},
function() {

    alert("i want to always run no matter what");
});

Solution 4 - Javascript

Just a note for those who use jQuery 3.0 and later

>Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

As in official documentation

Solution 5 - Javascript

There is a bug ajax is dependent on the server, need to check status with "complete" is the best, a kind of "success", "error" and others are not 100% of the PUT, POST and GET ... look at an example

$.ajax({
	url: '/api/v2/tickets/123456.json',
	....
	....
	....
	complete: function(data) { 
		if (data.statusText == "success") { 
			console.log("Sent successfully");
		} else { 
			console.log("Not Sent");
		}
	}
});

Sorry bad english! Cheer ;-)

Solution 6 - Javascript

if you want one code definition for all ajax requests, you can do it like this

$(document).ajaxComplete(function () {
    console.log('ajax complete on doc');
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOliver WatkinsView Question on Stackoverflow
Solution 1 - JavascriptRafael Gomes FranciscoView Answer on Stackoverflow
Solution 2 - JavascriptjrummellView Answer on Stackoverflow
Solution 3 - JavascriptFrances McMullinView Answer on Stackoverflow
Solution 4 - JavascriptJayavardhan GangeView Answer on Stackoverflow
Solution 5 - JavascriptKingRiderView Answer on Stackoverflow
Solution 6 - JavascriptsairfanView Answer on Stackoverflow