jQuery Deferred resolveWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

deferred.resolveWith(context [, args]) settles a Deferred as resolved (since jQuery 1.5) and runs done 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.resolveWith(context [, argsArray])

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

resolve("ok")
resolveWith(obj, ["ok"])   // same args, custom this

pair with: rejectWith / notifyWith
keep private; return promise()
CallWhat it does
contextBecomes this in done 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 done() handlers that use this if needed.
Step 3On success, call resolveWith(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 done()

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

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

dfd.done(function (result) {
  console.log(this.id + ": " + result);  // uploader: complete
});

dfd.resolveWith(ctx, ["complete"]);
Try It Yourself

How It Works

  1. Define context. ctx holds an id the handler needs.
  2. Attach done. The callback uses this.id and the result arg.
  3. Resolve with context. resolveWith(ctx, ["complete"]) sets this.

Example 2 — Multiple Values in the Args Array

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

jQuery
var dfd = $.Deferred();

dfd.done(function (id, name) {
  console.log(id + ": " + name);  // 42: Alex
});

dfd.resolveWith(null, [42, "Alex"]);
Try It Yourself

How It Works

  1. Plan parameters. Done expects (id, name).
  2. Wrap in an array. [42, "Alex"] is required for resolveWith.
  3. Context can be null. When you only need args, pass null as context.

Example 3 — resolve() vs resolveWith()

Same success values; only resolveWith controls this.

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

$.Deferred().done(function (msg) {
  console.log("resolve this.tag:", this.tag);  // undefined (usually)
}).resolve("ok");

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

How It Works

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

Frequently Asked Questions

resolveWith(context, args) settles the Deferred as resolved. Done callbacks from done() or then() run with this set to context, and parameters taken from the optional args array.
resolve(...values) passes arguments directly. resolveWith(ctx, [a, b]) lets you choose this and wrap values in an array — the same pattern as rejectWith() and notifyWith().
An optional array (or array-like) such as ["ok"] or [42, "Alex"]. jQuery spreads those values as separate parameters to each done callback.
When done 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 reject(), later resolveWith() is ignored.
Usually no. Keep the Deferred private and return promise() so outside code can listen with done/fail but cannot settle the operation.

Next: deferred.reject() Method

Learn how to read whether a Deferred is pending, resolved, or rejected.

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