jQuery Deferred always() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

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
});
CallWhat it does
Deferred / jqXHRObject that will settle
alwaysCallbacksFunction or array of functions
ReturnSame Deferred (chainable)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Create a Deferred (or use a jqXHR from $.ajax() / $.get()).
Step 2Register done() / fail() for success and error UI.
Step 3Register always(fn) for cleanup that must run either way.

Return Value

Return ValueDescription
jQuery objectMost 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.

jQuery
var dfd = $.Deferred();

dfd.done(function (value) {
  console.log("done:", value);
});

dfd.always(function () {
  console.log("always: request finished");
});

dfd.resolve("OK");
Try It Yourself

How It Works

  1. Create. $.Deferred() starts pending.
  2. Register. done for success; always for cleanup.
  3. Resolve. Success path runs first.

Example 2 — Cleanup After Reject

always() still runs when the Deferred is rejected — that is its main value.

jQuery
var dfd = $.Deferred();

dfd.fail(function (reason) {
  console.log("fail:", reason);
});

dfd.always(function () {
  console.log("always: cleanup runs anyway");
});

dfd.reject("Network error");
Try It Yourself

How It Works

  1. Register fail + always. Error UI and shared cleanup.
  2. Reject. Failure path triggers fail.
  3. Always still runs. Cleanup is not skipped on error.

Example 3 — Classic done / fail / always Chain

Mirror the AJAX pattern: handle data, handle errors, then finalize.

jQuery
var dfd = $.Deferred();

dfd
  .done(function (data) {
    console.log("Profile:", data.name);
  })
  .fail(function (err) {
    console.warn("Request failed:", err);
  })
  .always(function () {
    console.log("Re-enable submit button");
  });

dfd.resolve({ name: "Alex" });
// → Profile: Alex
// → Re-enable submit button
Try It Yourself

How It Works

  1. Chain three handlers. Success, error, cleanup.
  2. Resolve with data. done reads data.name.
  3. Always ignores args. Just restores UI state.

Frequently Asked Questions

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.

Next: deferred.catch() Method — Handle Rejections

Learn the modern shorthand for attaching a rejection handler on a Deferred.

deferred.catch() Method — Handle Rejections tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful