deferred.always() adds handlers that run when a Deferred is either resolved or rejected (since jQuery 1.6). Ideal for shared cleanup. Returns the Deferred so you can chain. Accepts a function or an array of functions.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
dfd.done(onSuccess) // resolve only
dfd.fail(onError) // reject only
dfd.always(onCleanup) // resolve OR reject
// Official AJAX-style cleanup:
$.get("test.php").always(function () {
// hide spinner — ignore args
});
Call
What it does
Deferred / jqXHR
Object that will settle
alwaysCallbacks
Function or array of functions
Return
Same Deferred (chainable)
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Create a Deferred (or use a jqXHR from $.ajax() / $.get()).
Step 2
Register done() / fail() for success and error UI.
Step 3
Register always(fn) for cleanup that must run either way.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
Example 1 — Cleanup After Resolve
Register done() for success and always() for shared cleanup.
always() registers a callback that runs when the Deferred settles — either resolved or rejected. Use it for shared cleanup such as hiding spinners or re-enabling buttons.
done() runs only on resolve; fail() only on reject. always() runs after either outcome. It does not run while the Deferred is still pending.
Usually no. Official docs warn that resolve and reject arguments are often different shapes. Prefer always() for side effects that ignore args; use done() or fail() when you need the well-known argument order.
Yes. jqXHR objects implement the Deferred interface, so you can chain .done(), .fail(), and .always() — a common pattern for UI cleanup.
Conceptually similar: both run after success or failure. always() is jQuery-specific; native finally() is the modern standard outside jQuery.
Yes. Pass multiple functions or call always() several times. Callbacks run in registration order when the Deferred settles.