Deferred()
Create objects
Learn how to create Deferred objects and control asynchronous operations with flexibility.

Asynchronous code runs in the background — AJAX calls, timers, animations. jQuery Deferred gives you a structured way to run code when those tasks succeed, fail, or update progress. This hub links to 18 Deferred method tutorials.
Create objects
Learn how to create Deferred objects and control asynchronous operations with flexibility.
Success & error
Handle success and failure callbacks efficiently to manage the outcome of your async tasks.
Cleanup
Execute code after success or failure. Perfect for cleanup operations and final steps.
Chain steps
Chain multiple asynchronous steps together and pass results between them seamlessly.
Read-only view
Work with a read-only promise interface for safer and more predictable code.
Full index
Explore all 18 Deferred method tutorials in a structured and beginner-friendly learning path.
jQuery Deferred is a powerful way to handle asynchronous operations in JavaScript. It provides a flexible API to manage tasks that may take time to complete, such as AJAX requests, animations, or custom functions.
It helps you write cleaner, more maintainable code by giving you better control over the flow of asynchronous tasks.
Asynchronous operations are everywhere in modern web development. Deferred helps you coordinate those operations efficiently and predictably.
Manage complex async flows with a simple and consistent API.
Chain operations, handle success or failure, and track progress with ease.
Gracefully handle errors and keep your application stable.
Works well with other jQuery features and third-party plugins.
In short: Deferred makes it easier to work with asynchronous tasks, so you can focus on building great features instead of worrying about callback complexity.
Create a Deferred, attach handlers, and settle it:
const dfd = $.Deferred();
dfd.done(function (data) {
console.log("Success:", data);
}).fail(function (err) {
console.error("Failed:", err);
}).always(function () {
console.log("Cleanup runs either way");
});
// Later, when async work finishes:
dfd.resolve("Hello from Deferred"); | Group | Examples | Purpose |
|---|---|---|
| Factory | $.Deferred() | Create a new Deferred object |
| Handlers | done(), fail(), always() | Register callbacks |
| Settlement | resolve(), reject() | Finish the async task |
| Chaining | then(), catch() | Pipeline transforms and errors |
| Progress | progress(), notify() | Interim status updates |
| Status | state(), promise() | Inspect or expose read-only access |
| Goal | Method |
|---|---|
| Create a Deferred | $.Deferred() |
| On success | dfd.done(fn) |
| On failure | dfd.fail(fn) |
| Cleanup either way | dfd.always(fn) |
| Chain steps | dfd.then(doneFn, failFn) |
| Mark success | dfd.resolve(value) |
| Mark failure | dfd.reject(reason) |
| Check status (jQuery 3+) | dfd.state() → pending | resolved | rejected |
| Share read-only access | dfd.promise() |
Deferred is useful when you need better control, structure, and reliability for asynchronous operations in your application.
Wrap setTimeout, third-party SDKs, or Web APIs in a Deferred so your app can consume uniformly.
Chain .done() / .fail() on $.ajax() without nested callbacks.
Coordinate code that runs when effects complete or fail.
Return promise() from a module so internals stay private.
Use notify() and progress() for upload bars or multi-step wizards.
Key benefit: Deferred gives you explicit control over success, failure, and progress—resulting in cleaner, more maintainable asynchronous code.
A Deferred moves through three states:
Search by method name or browse by category. Each tutorial includes syntax, five try-it examples, and FAQs.
Create Deferreds and attach the core success/failure listeners.
| Method | Description | Tutorial |
|---|---|---|
jQuery.Deferred() | Factory that creates a new Deferred object for custom async workflows. | Open |
done() | Register a callback that runs when the Deferred resolves successfully. | Open |
fail() | Register a callback that runs when the Deferred is rejected. | Open |
promise() | Return a read-only promise so callers cannot resolve or reject. | Open |
Register callbacks, chain transforms, and handle errors in pipelines.
| Method | Description | Tutorial |
|---|---|---|
always() | Register a callback that runs after resolve or reject — ideal for cleanup. | Open |
catch() | Handle rejection at the end of a promise chain (like Promise.catch). | Open |
then() | Chain success and failure handlers; return values flow to the next step. | Open |
pipe() | Legacy filter/chain API — deprecated in jQuery 3+; use then() instead. | Open |
progress() | Listen for interim progress updates from notify() or notifyWith(). | Open |
Report interim status before final resolve or reject.
| Method | Description | Tutorial |
|---|---|---|
notify() | Send a progress update while the Deferred is still pending. | Open |
notifyWith() | Send progress with a custom this context and argument array. | Open |
Resolve, reject, inspect status, and bind context with With-methods.
| Method | Description | Tutorial |
|---|---|---|
resolve() | Settle the Deferred as successful and pass results to done() handlers. | Open |
resolveWith() | Resolve with a custom this context and arguments in an array. | Open |
reject() | Settle the Deferred as failed and pass reasons to fail() handlers. | Open |
rejectWith() | Reject with a custom this context and arguments in an array. | Open |
state() | Return "pending", "resolved", or "rejected" — preferred status check in jQuery 3+. | Open |
isResolved() | Return true if the Deferred resolved (deprecated — prefer state()). | Open |
isRejected() | Return true if the Deferred rejected (deprecated — prefer state()). | Open |
Include jQuery 3.7+ and open DevTools Console (F12) to run each snippet.
Build a Deferred and settle it manually.
Simulate async work with setTimeout, then resolve with a result.
function fetchGreeting() {
const dfd = $.Deferred();
setTimeout(function () {
dfd.resolve("Hello, Deferred!");
}, 500);
return dfd.promise();
}
fetchGreeting().done(function (msg) {
console.log(msg);
}); The factory creates a Deferred; you return promise() so callers cannot call resolve themselves. Settlement happens inside your async function.
Classic AJAX-style handlers for success, error, and shared cleanup.
function loadData(shouldFail) {
const dfd = $.Deferred();
setTimeout(function () {
if (shouldFail) {
dfd.reject("Network error");
} else {
dfd.resolve({ id: 1, name: "Ada" });
}
}, 300);
return dfd.promise();
}
loadData(false)
.done(function (data) { console.log("Loaded:", data.name); })
.fail(function (err) { console.error(err); })
.always(function () { console.log("Request finished"); }); always() runs after either branch — perfect for hiding loading spinners. Pass true to loadData to see the fail path instead.
Pipeline values and hide settlement from consumers.
Transform resolved values through a pipeline — similar to native Promises.
$.Deferred()
.resolve(10)
.then(function (n) { return n * 2; })
.then(function (n) { return "Result: " + n; })
.done(function (msg) { console.log(msg); }); Each then() callback can return a new value that flows to the next step. Use the second argument or catch() for error handling in longer chains.
Keep resolve/reject private inside a module; return a read-only promise.
const Timer = (function () {
const dfd = $.Deferred();
setTimeout(function () {
dfd.resolve("Timer done");
}, 400);
// Public API — no resolve/reject exposed
return {
onComplete: dfd.promise()
};
})();
Timer.onComplete.done(function (msg) {
console.log(msg);
});
// Timer.onComplete.resolve("hack"); // undefined — safe! promise() strips settlement methods. External code can listen but cannot change the outcome — a simple encapsulation pattern.
Check whether a Deferred is still pending or has settled (jQuery 3+).
const dfd = $.Deferred();
console.log("Before:", dfd.state()); // pending
dfd.resolve("OK");
console.log("After resolve:", dfd.state()); // resolved
const dfd2 = $.Deferred();
dfd2.reject("Oops");
console.log("After reject:", dfd2.state()); // rejected state() replaces deprecated isResolved() / isRejected() with one clear string. A Deferred can only settle once — subsequent resolve/reject calls are ignored.
Real-world scenarios where this tool or approach can be applied to solve common problems efficiently.
Handle asynchronous HTTP requests without blocking the UI.
Example: Fetching data from APIs, submitting forms, loading content.
Coordinate complex animations sequentially or in parallel.
Example: Chaining animations, delaying effects, smooth transitions.
Build and manage custom APIs with multiple asynchronous steps.
Example: Validations, data processing, multi-step workflows.
Track upload progress and handle success or failure scenarios.
Example: Uploading files, showing progress bars, retry on failure.
Execute tasks in a specific order where each step depends on the previous one.
Example: Form wizards, step-by-step processes, data pipelines.
Run functions after a delay or at a specific time.
Example: Auto-save, notifications, timeouts, polling.
Pro Tip: These are just a few examples — explore and combine them to build powerful, responsive applications.
Why reach for Deferred? Here’s what it brings to your async code.
Separate success, failure, and cleanup instead of one giant callback.
Route failures to fail() or catch() consistently.
Integrates with jQuery AJAX and patterns familiar from native Promises.
Report interim status with notify() — something basic callbacks lack.
Pro Tip: Combine these strengths — handle success, failure, and progress in one place for maintainable async flows.
Follow these best practices to write clean, efficient, and maintainable code.
Begin with simple examples and gradually move to complex use cases.
Use each feature for a specific purpose and avoid overcomplicating your code.
Leverage chaining to write concise code, but keep it readable.
Always use .fail() or .catch() to handle errors gracefully.
Use .progress() to provide feedback for long-running operations.
Pro Tip: Practice regularly and explore real-world examples to master these concepts faster!
Avoid these common mistakes to write better, more reliable code.
Not handling errors can cause your application to break unexpectedly.
→ Always use .fail() or .catch() to handle errors gracefully.
Long-running tasks or synchronous operations can freeze the UI.
→ Use deferred objects and asynchronous patterns to keep the UI responsive.
Too many nested callbacks lead to “callback hell” and make code hard to read.
→ Use chaining and proper structure to keep your code clean and readable.
A deferred that is never resolved or rejected can leave your code in a pending state.
→ Always call resolve() or reject() in all code paths.
Forgetting to report progress in long-running tasks can lead to a poor user experience.
→ Use .progress() to keep users informed about ongoing operations.
Pro Tip: Write small, test often, and review your code to catch issues early.
$.Deferred() starts in the pending state with empty callback queues.
done, fail, always, and progress queue callbacks for later.
resolve or reject locks the state and drains the matching queues.
Callbacks run in order, UI stays unblocked, and errors have a clear path.
jQuery Deferred ships with jQuery 1.5+ and is fully supported in jQuery 3.7.x. Methods like state() and catch() align with jQuery 3+. For greenfield projects, also consider native Promise — but Deferred remains essential when using jQuery AJAX and legacy plugins.
Available wherever jQuery runs — all modern browsers and IE 9+ with a supported jQuery build. Match your jQuery version to project requirements.
Bottom line: Safe for jQuery-based apps. Prefer state() and then() in jQuery 3+ code; treat pipe() as legacy.
jQuery Deferred is a powerful tool for managing asynchronous tasks — from AJAX to animations to custom APIs.
Start with $.Deferred(), done(), resolve(), then explore chaining and progress as you grow.
Use the searchable index to open all 18 tutorials — each includes try-it labs and FAQs.
promise() from public APIsalways() for shared cleanupstate()then()pipe() in new jQuery 3 codeYour gateway to 18 method tutorials.
Create async tasks
StartSuccess & error
HandlersEither outcome
CleanupChain values
PipelineSearch all
RefYou've been using Deferreds without realizing it! Every $.ajax() secretly hands you a Promise-like object — so you can chain .done() and .fail() right onto your calls. No $.Deferred() required. Sneaky, right? 😏
Learn how to run cleanup code whether an async task succeeds or fails.
10 people found this page helpful