.always()
.always() fires whether the Deferred resolves or rejects — the equivalent of finally in try/catch. Perfect for hiding spinners and re-enabling buttons.
.always() fires whether the Deferred resolves or rejects — the equivalent of finally in try/catch. Perfect for hiding spinners and re-enabling buttons.
$("#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);
});
Always put UI cleanup (hide spinner, re-enable button) in .always() — not in .done() alone.