Home jQuery Deferred reject() jQuery Deferred reject() Method Definition
What Is reject()? deferred.reject([args]) settles a Deferred as rejected (since jQuery 1.5) and runs fail callbacks from fail() or then() . Optional args are passed to those handlers. It returns the same Deferred for chaining.
deferred.reject([args…])
reject() → settle as failed (owner only)
fail() → listen for rejection
resolve() → settle as success (opposite)
state locks after reject — once only
return promise() so callers cannot reject Approach
How to Use reject() Own the Deferred; reject when the work fails; let others listen.
Create a Deferred (keep it private in your helper). Attach fail() / then() (or return promise() for callers). On failure, call reject(args…) with a useful reason. Do not call resolve() afterward — the Deferred is already settled. Piece Required? Role argsOptional Values passed to every fail callback Return — Same Deferred (chainable) Since — jQuery 1.5
Pseudocode dfd = Deferred
handlers = fail callbacks (from fail / then)
reject(args…):
if dfd already settled: return // ignore
state = "rejected"
remember args
for each fail handler (in add order):
handler(args…)
later fail(fn):
if already rejected: fn(args…) immediately
else: queue fn Reference
Cheat Sheet Keep these facts handy when signaling async failure.
Topic Detail Syntax deferred.reject([args…])Triggers fail(), rejection branch of then(), always()State "rejected" — settles onceLate fail() Runs immediately with the reject args vs fail() reject fires; fail listensvs resolve() Failure vs success settlement Since jQuery 1.5
Try it
Live Preview See how reject() drives fail() and once-only settlement.
Code
jQuery Examples Short, copy-ready snippets. Open the browser console, or use Try It Yourself for a live editor.
1. reject + fail 2. Multiple args 3. Late fail() 4. Once settled 5. Reject behind promise() Example 1 — reject() + fail() Settle as failed and read the reason in a fail handler.
var dfd = $.Deferred();
dfd.fail(function (reason) {
console.log(reason); // Network timeout
});
dfd.reject("Network timeout"); How It Works Register fail. The handler waits for rejection.Call reject. Pass a clear string reason.Handler runs. It receives that same string.State locks. The Deferred is now "rejected".Example 2 — Multiple Reject Arguments Pass several values; every fail callback receives them in order.
var dfd = $.Deferred();
dfd.fail(function (code, message) {
console.log(code + ": " + message); // 404: Not found
});
dfd.reject(404, "Not found"); How It Works Plan the shape. Fail handlers expect (code, message).Reject with two args. reject(404, "Not found").Receive both. The callback logs them together.Keep it consistent. Use the same arg order everywhere you reject.Example 3 — Late fail() After Reject Handlers added after rejection run immediately with the reject args.
var dfd = $.Deferred();
dfd.reject("already failed");
dfd.fail(function (reason) {
console.log(reason); // already failed
}); How It Works Reject first. State becomes rejected; args are stored.Attach fail late. Official rule: it runs immediately.Same args. You still get "already failed".Useful for UI. Late listeners still see the final error.Example 4 — Settle Once (Reject After Resolve Ignored) After resolve(), a later reject() does nothing.
var dfd = $.Deferred();
var lines = [];
dfd.done(function (v) { lines.push("done: " + v); });
dfd.fail(function (v) { lines.push("fail: " + v); });
dfd.resolve("ok");
dfd.reject("nope"); // ignored
console.log(lines.join(" | ")); // done: ok
console.log(dfd.state()); // resolved How It Works Attach both paths. done and fail are ready.Resolve first. State becomes "resolved"; done runs.Reject later. Ignored — fail never runs.Rule. First settle wins; the other is a no-op.Example 5 — Reject Behind promise() Only the owner rejects; callers listen on the returned Promise.
function loadFile(ok) {
var dfd = $.Deferred();
setTimeout(function () {
if (ok) dfd.resolve("file.txt");
else dfd.reject("missing file");
}, 200);
return dfd.promise();
}
loadFile(false).fail(function (err) {
console.log(err); // missing file
}); How It Works Private Deferred. Only loadFile can call reject.Return Promise. Callers get promise() , not settle methods.Fail path. ok is false → reject("missing file").Listen outside. fail on the Promise prints the error. Edge Cases & Pitfalls Check these before relying on reject() in real projects.
once Settles only once After resolve or reject, further settle calls are ignored. Check state() if you need to know which won.
owner Only the owner should reject Official guidance: return promise() so other code cannot change state with reject().
args Pass useful error info Empty reject() works but hurts debugging. Prefer a message, code, or Error-like object.
Compatibility
Browser Support deferred.reject() needs jQuery 1.5+ loaded. It works across jQuery 1.5–3.x as the standard way to settle a Deferred as failed.
✓ jQuery 1.5+
deferred.reject() Works in Chrome, Firefox, Safari, and Edge when jQuery 1.5+ is loaded. Pair with fail() or then() to handle the rejection.
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 reject() Universal
Bottom line: Safe wherever jQuery Deferreds exist. Keep reject() private; expose promise() to callers.
Analysis
Performance Notes Approach Cost Best for reject(args)O(handlers) Normal failure settlement Many fail handlers Each runs once Keep handlers small Reject after settle No-op Harmless but wasteful — avoid
Rejection cost is dominated by your fail handlers (UI updates, logging), not by reject() itself. Pass compact error values, not huge objects, unless needed.
Remember
Key Takeaways Role: settle a Deferred as failed and run fail callbacks.
Pair: reject fires; fail / then listen.
Once: after settle, later reject/resolve are ignored.
Safety: return promise() so callers cannot reject.
One line: reject(args) marks failure — keep it private, pass clear reasons, listen with fail().
Frequently Asked Questions What does deferred.reject() do? reject() settles the Deferred as failed and runs failCallbacks from fail() or then(). Optional args are passed to those handlers. The state becomes rejected and stays locked.
How is reject() different from fail()? reject() is called by the Deferred owner to signal failure. fail() registers a listener that runs when rejection happens. reject fires; fail listens.
Can I reject after resolve? No. A Deferred settles once. After resolve(), later reject() is ignored. After reject(), later resolve() is ignored.
What can I pass to reject()? Any values — a string, Error, status code, or multiple arguments. Each fail callback receives the same args from the reject() call.
What if I add fail() after reject()? Late fail() handlers run immediately with the arguments that were passed to reject() — same idea as late done() after resolve().
Should callers be able to reject()? Usually no. Keep the Deferred private and return promise() so outside code can attach fail/done but cannot settle the operation.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion