jQuery $.Deferred() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is $.Deferred()?

$.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().

Remember
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();
NeedUse
Create a Deferred$.Deferred()
Mark successdfd.resolve(value)
Mark failuredfd.reject(reason)
Export read-only APIdfd.promise()

How to Use $.Deferred()

Create, attach handlers, settle when the work finishes.

  1. Call $.Deferred() (optionally with a beforeStart function).
  2. Attach done / fail / then / always handlers.
  3. Run your async or sync work.
  4. Call resolve() or reject() once when finished.
  5. Return promise() if other code should listen but not settle.
PieceRequired?Role
beforeStartNoRuns with the new Deferred as this and as arg 1
HandlersUsuallydone, fail, then, always, …
Return—New Deferred object

Pseudocode

Pseudocode
Deferred([beforeStart]):
  create object in state = pending
  if beforeStart: call it(deferred)
  return deferred

on resolve(args):
  if already settled: ignore
  state = resolved
  run done / then success / always queues

on reject(args):
  if already settled: ignore
  state = rejected
  run fail / then fail / always queues

promise():
  return read-only view (no resolve / reject)

Cheat Sheet

Core facts for the Deferred factory.

TopicDetail
SyntaxjQuery.Deferred( [ beforeStart ] )
Alias$.Deferred( [ beforeStart ] )
Starts aspending
Successresolve() / resolveWith()
Failurereject() / rejectWith()
Safe exportpromise() (no settle methods)
Late handlersStill run immediately if already settled
SincejQuery 1.5

Live Preview

Create a Deferred and compare resolve, reject, locked state, and promise().

Browser demo of $.Deferred — same ideas as the examples below.

Result
done: Hello
state: resolved

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.

Example 4 — Async Wrapper

Wrap delayed work that can succeed or fail; return a promise.

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

  setTimeout(function () {
    if (ok) {
      dfd.resolve(42);
    } else {
      dfd.reject("Service down");
    }
  }, 200);

  return dfd.promise();
}

fetchTotal(true)
  .done(function (n) { console.log("Total:", n); })
  .fail(function (err) { console.log("Error:", err); });
// Total: 42
Try It Yourself

How It Works

  1. Factory inside. Each call gets its own Deferred.
  2. Branch. Resolve with data or reject with a reason.
  3. Chain. Callers use done / fail (or then / catch).

Example 5 — Optional beforeStart

Pass a function to $.Deferred(); it runs with the new Deferred as this.

jQuery
var dfd = $.Deferred(function (deferred) {
  // deferred === this
  setTimeout(function () {
    deferred.notify("working…");
    deferred.resolve("task complete");
  }, 200);
});

dfd.progress(function (msg) {
  console.log("progress:", msg);
});

dfd.done(function (result) {
  console.log("done:", result);
});
// progress: working…
// done: task complete
Try It Yourself

How It Works

  1. beforeStart. Runs once before the factory returns the Deferred.
  2. Attach outside. Handlers can still be added after construction.
  3. Notify then resolve. Progress fires first; then done runs.

Edge Cases & Pitfalls

Watch these when building custom Deferred helpers.

locked

Second resolve / reject is ignored

After the first settle, the state never changes. Extra resolve() calls do nothing.

export

Return promise(), not the Deferred

Handing out the full Deferred lets callers settle your task early. Prefer promise().

late

Handlers after settle still run

Adding done() to an already-resolved Deferred runs the callback immediately with the stored args.

one

One Deferred per operation

Reuse a settled Deferred for a new request only if you create a fresh one — never “reset” an old object.

native

Not the same as native Promise

jQuery Deferred is Promises/A-inspired and jQuery-specific. Prefer Deferred APIs inside jQuery code; use native Promise when you leave the jQuery stack.

Browser Support

$.Deferred() is part of jQuery’s Deferred API (since 1.5). It works in any browser where your jQuery build runs.

✓ jQuery 1.5+ · Browsers

jQuery $.Deferred()

Supported wherever jQuery 1.5+ is loaded — Chrome, Firefox, Safari, Edge, and other browsers with a matching jQuery build.

100% With jQuery 1.5+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.Deferred() jQuery 1.5+

Bottom line: Use $.Deferred() whenever you need jQuery-style async control. Load jQuery 1.5+ (3.x recommended for modern projects).

Performance Notes

OperationCostBest for
$.Deferred()O(1) createOne object per async task
Attach handlersO(1) eachQueuing done / fail / then
resolve / rejectO(n) handlersRunning queued callbacks once

Creating a Deferred is cheap. Cost grows with how many handlers you attach and how heavy each callback is. Keep settle handlers short — update UI, map a value, or forward an error message.

Key Takeaways

  • Role: factory for a new Deferred representing one async unit of work.
  • Settle once: resolve or reject locks the state forever.
  • Export: return promise() so callers listen but cannot settle.
  • Since: jQuery 1.5 — the foundation for ajax-style Deferred APIs.

One line: $.Deferred() creates the object you resolve or reject — return promise() when sharing it.

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: deferred.done()

Attach success callbacks that run when a Deferred is resolved.

done() tutorial →

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