jQuery Deferred fail() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

deferred.fail(failCallbacks) adds handlers that run when a Deferred is rejected (failure). Available since jQuery 1.5. It returns the Deferred so you can chain more methods.

Syntax

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

jQuery
dfd.fail(fn)
dfd.fail(fn1, fn2)
dfd.fail([fn1, fn2], fn3)

reject("err")  → fail handlers run with "err"
resolve(...)   → fail does NOT run

$.get(url).fail(fn)  // jqXHR error
CallWhat it does
Deferred / jqXHRObject that can reject
failCallbacksFunction or array of functions
Return valueSame Deferred (chainable)

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

Parameters

ParameterDescription
Step 1Get a Deferred (e.g. $.Deferred()) or a jqXHR from $.ajax / $.get.
Step 2Call .fail(function (reason) { ... }) — pass a function or an array of functions.
Step 3When failure happens, call reject(reason) (or let AJAX fail).

Return Value

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

jQuery Examples

Example 1 — Basic Error Handler

Attach fail(), then reject() with a reason.

jQuery
var dfd = $.Deferred();

dfd.fail(function (error) {
  console.log("fail: " + error);
});

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

How It Works

  1. Create. $.Deferred() starts in the pending state.
  2. Register. fail(fn) queues the error callback.
  3. Reject. reject("Network error") settles as failure.

Example 2 — Multiple Handlers

Pass functions or arrays; they run in registration order (per the API docs).

jQuery
var dfd = $.Deferred();
var lines = [];

function logErr(msg) { lines.push("log: " + msg); }
function showErr(msg) { lines.push("ui: " + msg); }
function track(msg) { lines.push("track: " + msg); }

dfd.fail([logErr, showErr], track);

dfd.reject("timeout");
console.log(lines.join(" | "));
// log: timeout | ui: timeout | track: timeout
Try It Yourself

How It Works

  1. Arrays allowed. [logErr, showErr] adds both callbacks in order.
  2. Extra args. track also receives "timeout".
  3. Order. Handlers fire in the order they were added.

Example 3 — Multiple Reject Arguments

reject can pass several values — same shape as jqXHR .fail(xhr, status, error).

jQuery
var dfd = $.Deferred();

dfd.fail(function (xhr, status, error) {
  console.log(status);       // error
  console.log(error);        // Not Found
  console.log(xhr.status);   // 404
});

dfd.reject({ status: 404 }, "error", "Not Found");
Try It Yourself

How It Works

  1. Multi-arg reject. jQuery forwards every argument to each fail callback.
  2. AJAX shape. Real $.ajax().fail often gets (jqXHR, textStatus, errorThrown).
  3. Parameter list. Match them left-to-right in your handler.

Frequently Asked Questions

fail() registers one or more callbacks that run when the Deferred is rejected (failure). Handlers receive the values passed to reject() or rejectWith(), in registration order.
fail() runs only on reject. done() runs only on resolve. Pair them (or use then with two callbacks) so you cover both outcomes.
In jQuery 3.0+, catch() is an alias for fail() on Deferred and Promise objects. They behave the same — catch() exists for Promise-style naming.
Yes. Pass functions or arrays of functions. Callbacks run in the order they were added. You can also chain .fail().fail().
Yes. If the Deferred is already rejected, new fail() handlers fire immediately with the rejection reason.
Yes. jqXHR objects support .fail(function(xhr, status, error) { ... }) — the classic jQuery AJAX error-handler pattern.

Next: deferred.progress() Method

Check whether a Deferred has already been rejected.

deferred.progress() Method 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