0

According to the official JQuery documentation:

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.

And let's say that I have the following ajax script :

$.ajax({ url: 'myPHPScript.php', type: 'POST', data: { param_1: 'value_1', param_n: 'value_n'… }, username: 'myLogin', password: 'myPassword', beforeSend: function() { alert('The object was created but not yet initilized'); } }).done(function(data, textStatus, jqXHR) { alert('All the request was sent and we received data'); }).fail(function(jqXHR, textStatus, errorThrown) { alert('Error: the following error was occurred: ' + textStatus + ' Status : ' + jqXHR.Status); }).always(function() { // Here is my problem }); 

In the .always() function, how can I specify a different function for each statement, I mean when the Deferred is resolved, the always() function gets passed the following params (data, textStatus, jqXHR) however if the deferred is rejected it gets passed (jqXHR, textStatus, errorThrown).

Thanks

3
  • Surely that's what .done() and .fail() are for, you can have multiple such callbacks. Commented Jul 15, 2013 at 14:07
  • @Orbling, thanks for your replay, I know that each one off .done() and .fail() does a portion of what I am looking for, but as you may know .fail() is executed only and only if the other didn't success, in other means they are not executed together each time, however, .always() is all the time called, that's why it is important, but my problem as I said is to make a separate portion of code to be executed per differed status. Commented Jul 15, 2013 at 15:26
  • Could you not just check the first argument to see if it was a jqXHR then? Commented Jul 15, 2013 at 21:11

1 Answer 1

3

The only good solution is not using the arguments in always - if you need code specific to the success/failure of the AJAX call out them in done or fail callbacks. Usually the always callback is a place where you do things such as re-enabling a button, hiding a throbber, etc. All those are things where you do not care about the result of the request.

Besides that, you could check arguments.length for the number of arguments and then access the arguments via arguments[0] etc. But relying on the argument count is a bad idea since it might not be future-proof.

Sign up to request clarification or add additional context in comments.

7 Comments

@TheifMaster, thanks for your replay, but if I can re-enable a button, hiding a throbber, etc. in the .done() with a conditional test like : If(jqXHR.status===200) { // Re-enabling button, hiding a throbber, etc. } for example What is the necessity to have .always() function? If so, I think .always() is not necessary and should be removed from JQuery.
@ghaliloo Because always() always runs, while done() only runs if the deferred object is resolved (i.e. the ajax query was successful)
@ghaliloo No, that would only re-enable the button if the request succeeded. ThiefMaster wants to re-enable the button after the request regardless of whether it succeeded or not, and that's the use case for .always().
@ghaliloo There is no requirement to use the always function. If it is not suitable in your case, don't use it.
@Mike, hhhhhhh hhhhhh, No, if it wasn't suitable in my case I wouldn't make a new post about it, in contrary it is very useful for me, since I want to do a task if the Differed is resolved and another if it is rejected and I have to make it in .always() as I said because it is always called, however if I put in .done() or .fail(), I have no warranty it will be executed.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.