jQuery $.Deferred() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

$.Deferred() (same as jQuery.Deferred()) is a factory that returns a new Deferred object (since jQuery 1.5). Officially it creates a chainable utility for registering callbacks, invoking them, and relaying success or failure of sync or async work — the same pattern used by $.ajax().

Syntax

Start with these common calls. Keep them short and readable.

jQuery
var dfd = $.Deferred();   // pending

dfd.done(fn);             // success queue
dfd.fail(fn);             // failure queue
dfd.resolve(value);       // → resolved (done runs)
dfd.reject(reason);       // → rejected (fail runs)

// Share safely with callers:
return dfd.promise();
CallWhat it does
$.Deferred()Learn jQuery $.Deferred(): create Deferred objects, resolve and reject async work, return promise(), optional beforeStart callback, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Call $.Deferred() (optionally with a beforeStart function).
Step 2Attach done / fail / then / always handlers.
Step 3Run your async or sync work.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Create and Resolve

Factory → attach done() → resolve() with a value.

jQuery
var dfd = $.Deferred();

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

dfd.resolve("Hello from Deferred");
// done: Hello from Deferred
Try It Yourself

How It Works

  1. Create. $.Deferred() starts in pending.
  2. Queue. done() registers a success handler.
  3. Settle. resolve(...) locks the state and runs the queue.

Example 2 — Create and Reject

Same factory; use fail() and reject() for the error path.

jQuery
var dfd = $.Deferred();

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

dfd.reject("Validation failed");
// fail: Validation failed
Try It Yourself

How It Works

  1. Create. Fresh Deferred, still pending.
  2. Queue. fail() waits for rejection.
  3. Settle. reject(...) runs fail handlers; done is skipped.

Example 3 — Return promise()

Keep the Deferred private; callers only get a read-only promise.

jQuery
function loadConfig() {
  var dfd = $.Deferred();

  setTimeout(function () {
    dfd.resolve({ theme: "dark", lang: "en" });
  }, 300);

  return dfd.promise();  // no resolve / reject for callers
}

loadConfig().done(function (cfg) {
  console.log(cfg.theme, cfg.lang);
});
// dark en
Try It Yourself

How It Works

  1. Own the Deferred. Only loadConfig can settle it.
  2. Export. promise() lets callers attach done / fail.
  3. Protect. Outside code cannot call resolve early by accident.

Frequently Asked Questions

$.Deferred() (same as jQuery.Deferred()) is a factory that creates a new Deferred object — a chainable handle for one async operation you control with resolve(), reject(), and callback methods like done() and fail().
A Deferred can resolve or reject itself. deferred.promise() returns a read-only Promise that exposes done/fail/then but hides resolve/reject — return that when sharing async work with other code.
pending (not finished), resolved (success), or rejected (failure). After resolve or reject, the state is locked; further settle calls are ignored.
Yes. An optional beforeStart function runs just before the factory returns, with the new Deferred as this and as the first argument. Use it to attach early callbacks or start async setup.
jqXHR from $.ajax() implements the Deferred interface. $.Deferred() lets you build the same callback patterns for your own timers, validators, and helpers.
Deferred objects were added in jQuery 1.5 and remain available in modern 1.x, 2.x, and 3.x builds.

Next: $.when() Method — Combine Deferreds

Combine several Deferreds or promises into one aggregate promise.

$.when() Method — Combine Deferreds 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.

8 people found this page helpful