jQuery Deferred rejectWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

deferred.rejectWith(context [, args]) settles a Deferred as rejected (since jQuery 1.5) and runs fail callbacks with this set to context. Optional args is an array whose values become the callback parameters. It returns the same Deferred for chaining.

Syntax

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

jQuery
deferred.rejectWith(context [, argsArray])

context  → this inside fail callbacks
args     → array → separate parameters

reject("msg")
rejectWith(obj, ["msg"])   // same args, custom this

pair with: resolveWith / notifyWith
keep private; return promise()
CallWhat it does
contextBecomes this in fail callbacks
argsArray of values passed as parameters
ReturnSame Deferred (chainable)
SincejQuery 1.5

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

Parameters

ParameterDescription
Step 1Create a Deferred (keep it private in your helper).
Step 2Attach fail() handlers that use this if needed.
Step 3On failure, call rejectWith(context, [arg1, arg2, …]).

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

Example 1 — Custom this in fail()

Pass a context object so the fail handler can read this.id.

jQuery
var ctx = { id: "uploader" };
var dfd = $.Deferred();

dfd.fail(function (reason) {
  console.log(this.id + ": " + reason);  // uploader: timeout
});

dfd.rejectWith(ctx, ["timeout"]);
Try It Yourself

How It Works

  1. Define context. ctx holds an id the handler needs.
  2. Attach fail. The callback uses this.id and the reason arg.
  3. Reject with context. rejectWith(ctx, ["timeout"]) sets this.

Example 2 — Multiple Values in the Args Array

Put several failure values in an array; they become separate parameters.

jQuery
var dfd = $.Deferred();

dfd.fail(function (code, message) {
  console.log(code + ": " + message);  // 404: Not found
});

dfd.rejectWith(null, [404, "Not found"]);
Try It Yourself

How It Works

  1. Plan parameters. Fail expects (code, message).
  2. Wrap in an array. [404, "Not found"] is required for rejectWith.
  3. Context can be null. When you only need args, pass null as context.

Example 3 — reject() vs rejectWith()

Same failure values; only rejectWith controls this.

jQuery
var ctx = { tag: "A" };

$.Deferred().fail(function (msg) {
  console.log("reject this.tag:", this.tag);  // undefined (usually)
}).reject("oops");

$.Deferred().fail(function (msg) {
  console.log("rejectWith:", this.tag, msg);  // A oops
}).rejectWith(ctx, ["oops"]);
Try It Yourself

How It Works

  1. Plain reject. Args are fine; this is not your object.
  2. rejectWith. Same message, but this is ctx.
  3. Choose by need. Use reject for simple reasons; rejectWith when methods need this.

Frequently Asked Questions

rejectWith(context, args) settles the Deferred as rejected. Fail callbacks from fail() or then() run with this set to context, and parameters taken from the optional args array.
reject(...values) passes arguments directly. rejectWith(ctx, [a, b]) lets you choose this and wrap values in an array — the same pattern as resolveWith() and notifyWith().
An optional array (or array-like) such as ["timeout"] or [404, "Not found"]. jQuery spreads those values as separate parameters to each fail callback.
When fail handlers are methods on a widget or service object and need this to point at that object — especially inside timers or nested callbacks where this would otherwise be lost.
No. A Deferred settles once. After resolve(), later rejectWith() is ignored.
Usually no. Keep the Deferred private and return promise() so outside code can listen with fail/done but cannot settle the operation.

Next: deferred.state() Method

Learn how to settle a Deferred successfully and run done handlers.

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