jQuery $.Deferred() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Async factory

What You’ll Learn

$.Deferred() is the factory that creates jQuery Deferred objects — the building blocks behind $.ajax() callbacks, custom async helpers, and promise-style chains. This tutorial covers creation, resolve/reject, returning promise(), and five hands-on examples.

01

Create

$.Deferred()

02

Resolve

Success path

03

Reject

Failure path

04

promise()

Read-only export

05

States

pending → settled

06

Since 1.5

Core jQuery API

Introduction

Asynchronous programming keeps web pages responsive while network calls, timers, and animations run in the background. jQuery has supported this pattern since version 1.5 with Deferred objects — and $.Deferred() is how you create them.

Think of a Deferred as a ticket for work that will finish later. You hand the ticket to callers so they can register done() and fail() handlers; when your async task completes, you call resolve() or reject() to settle the ticket.

Understanding the $.Deferred() Method

$.Deferred() (identical to jQuery.Deferred()) is a constructor-style factory — not a method on an existing Deferred. Each call returns a fresh Deferred object representing one async operation with three possible states:

  • pending — work still running; callbacks are queued.
  • resolved — success; done() and always() handlers run.
  • rejected — failure; fail(), catch(), and always() handlers run.

Once resolved or rejected, the state is frozen — calling resolve() again has no effect. That guarantee makes Deferred objects predictable in chained workflows.

💡
Beginner Tip

When you return async work from your own function, create a Deferred internally, call resolve or reject when finished, and return dfd.promise() so outside code cannot accidentally resolve your task early.

📝 Syntax

General forms of the Deferred factory:

jQuery
jQuery.Deferred( [ beforeStart ] )

// Common alias:
const dfd = $.Deferred();

Parameters

  • beforeStart (optional) — function invoked immediately with the new Deferred as this. Use it to start async setup in one place.

Return value

  • A new Deferred object with methods such as resolve, reject, done, fail, always, then, and promise.

Minimal resolve example

jQuery
const deferred = $.Deferred();

// …perform async work…

deferred.resolve(result);  // settle as success

⚡ Quick Reference

GoalCode
Create Deferredconst dfd = $.Deferred()
Mark successdfd.resolve(value)
Mark failuredfd.reject(reason)
Export read-only APIreturn dfd.promise()
Check statedfd.state()"pending" etc.

📋 Deferred vs promise()

Same object, two levels of access — producer vs consumer.

Deferred
$.Deferred()

Full control — resolve/reject

promise()
dfd.promise()

Listen only — no resolve

Producer
your module

Keeps Deferred private

Consumer
.done/.fail

Registers callbacks

Examples Gallery

Each example creates a Deferred from scratch. Use DevTools or the Try-it links with jQuery 3.7+ loaded from the CDN.

📚 Getting Started

Create a Deferred and settle it with resolve() or reject().

Example 1 — Create and Resolve

Build a Deferred, attach done(), then call resolve() with a value.

jQuery
const dfd = $.Deferred();

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

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

How It Works

$.Deferred() returns an object in the pending state. resolve() moves it to resolved and invokes queued done() callbacks with your value.

Example 2 — Create and Reject

The same factory supports failure via reject() and fail() handlers.

jQuery
const dfd = $.Deferred();

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

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

How It Works

Every Deferred starts pending. You choose exactly one settlement: resolve or reject. After rejection, done() handlers are skipped.

📈 Practical Patterns

Encapsulate async work the way production jQuery plugins do.

Example 3 — Return promise() to Callers

Keep the Deferred private; expose only the read-only promise interface.

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

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

  return dfd.promise();  // callers cannot call resolve()
}

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

How It Works

promise() returns an object with callback methods but without resolve or reject. That prevents consumers from marking your task complete prematurely.

Example 4 — Async Wrapper Function

Wrap timer-based or callback-based APIs in a reusable function that returns a promise.

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

  setTimeout(function () {
    if (Math.random() > 0.3) {
      dfd.resolve(42);
    } else {
      dfd.reject("Service down");
    }
  }, 700);

  return dfd.promise();
}

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

How It Works

This pattern mirrors how you would wrap legacy callback APIs before native Promises were common — one Deferred per operation, settled when the underlying work finishes.

Example 5 — Optional Constructor Callback

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

jQuery
const dfd = $.Deferred(function (deferred) {
  deferred.notify("working…");

  setTimeout(function () {
    deferred.resolve("task complete");
  }, 400);
});

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

dfd.done(function (result) {
  console.log("done:", result);
});
Try It Yourself

How It Works

The optional callback starts work as soon as the Deferred exists. Pair it with notify() and progress() for long-running tasks — see the progress tutorial for deeper coverage.

🚀 Use Cases

  • AJAX wrappers — unify success and error callbacks before fetch was standard.
  • Animation coordination — resolve a Deferred when a .fadeOut() animation completes.
  • Parallel tasks — combine multiple Deferreds with $.when() (covered on the Deferred hub).
  • Timeouts — reject if work exceeds a deadline using setTimeout plus reject().
  • Plugin APIs — return promise() from jQuery plugins so callers chain done() naturally.

🧠 Deferred Lifecycle

1

Create

const dfd = $.Deferred() — state is pending.

Factory
2

Register

Callers attach done, fail, or always handlers while pending.

Queue
3

Settle

You call resolve(value) or reject(reason) once work finishes.

Settled
=

Callbacks run

Handlers fire in order; chains continue via then() if you return new promises.

📝 Notes

  • $.Deferred() and jQuery.Deferred() are the same function.
  • Resolve or reject exactly once; subsequent calls are ignored.
  • Handlers registered after settlement still run immediately (if they match the state).
  • Prefer returning promise() from public APIs to hide resolve/reject.
  • Modern greenfield apps often use native Promise, but jQuery Deferred remains useful in legacy codebases.

Browser Support

jQuery.Deferred() has been part of jQuery since version 1.5. It runs wherever jQuery runs — no separate polyfill required.

jQuery 1.5+

jQuery.Deferred()

Available in jQuery 1.5+, 2.x, and 3.x across all browsers supported by your jQuery build.

100% With jQuery loaded
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() Universal

Bottom line: Load jQuery 3.7+ for the latest Promises/A+ helpers (then, catch). The constructor itself works in every jQuery version that ships Deferred.

🎉 Conclusion

$.Deferred() is the starting point for custom async workflows in jQuery. Create an object, register callbacks, settle with resolve or reject, and optionally export promise() to callers.

Master this factory and the rest of the Deferred API — done, fail, always, and then — clicks into place.

💡 Best Practices

✅ Do

  • Return promise() from public functions
  • Name Deferred variables clearly (loadDfd)
  • Handle errors with fail() or catch()
  • Chain with then() for multi-step flows
  • Report progress for long tasks via notify()

❌ Don’t

  • Expose resolve/reject to untrusted callers
  • Resolve and reject the same Deferred
  • Forget to settle — pending Deferreds hang forever
  • Mix unrelated async tasks on one Deferred
  • Assume Deferred replaces try/catch for sync code

Key Takeaways

Knowledge Unlocked

Five things to remember about $.Deferred()

The factory behind jQuery async patterns.

5
Core concepts
02

resolve()

Success

Settle
03

reject()

Failure

Settle
🔒 04

promise()

Read-only

Export
1.5 05

Since 1.5

Core API

History

❓ Frequently Asked Questions

$.Deferred() (same as jQuery.Deferred()) is a factory function that creates a new Deferred object — a stateful handle for an async operation you control with resolve(), reject(), and callback methods like done() and fail().
A Deferred is the full object: you can resolve or reject it. promise() returns a read-only view that exposes done/fail/then but hides resolve/reject — use it when returning async work to other code.
pending (not finished), resolved (success), or rejected (failure). After resolving or rejecting, the state is locked — you cannot change it again.
Yes. An optional callback runs immediately with the new Deferred as this. It is useful for kicking off async setup and calling resolve/reject when finished.
jqXHR objects from $.ajax() implement the Deferred interface. $.Deferred() lets you build the same callback patterns for your own async functions.
Deferred objects were added in jQuery 1.5. They are available in all modern jQuery 1.x, 2.x, and 3.x releases.
Did you know?

jQuery introduced Deferred objects in 2011 (jQuery 1.5) partly to unify the messy callback options on $.ajax() — the same object you create with $.Deferred() powers jqXHR under the hood.

Continue to done()

Learn how success callbacks attach to a settled Deferred.

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.

6 people found this page helpful