SyntaxStudy
Sign Up
jQuery Beginner 3 min read

.always() for Cleanup

.always()

.always() fires whether the Deferred resolves or rejects — the equivalent of finally in try/catch. Perfect for hiding spinners and re-enabling buttons.

Example
$("#submitBtn").prop("disabled", true);
showSpinner();

$.post("/api/save", formData)
  .done(function(res)  { showSuccess(res.message); })
  .fail(function(xhr)  { showError(xhr.responseJSON.error); })
  .always(function()   {
    hideSpinner();
    $("#submitBtn").prop("disabled", false);
  });
Pro Tip

Always put UI cleanup (hide spinner, re-enable button) in .always() — not in .done() alone.