jQuery Deferred resolveWith() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is resolveWith()?

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.

Remember
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()
NeedUse
Success with custom thisdfd.resolveWith(obj, [value])
Success without caring about thisdfd.resolve(value)
Fail with custom thisdfd.rejectWith(obj, [reason])

How to Use resolveWith()

Pick a context object, wrap success values in an array, then resolve.

  1. Create a Deferred (keep it private in your helper).
  2. Attach done() handlers that use this if needed.
  3. On success, call resolveWith(context, [arg1, arg2, …]).
  4. Return promise() to callers so they cannot settle the Deferred.
PieceRequired?Role
contextYesBecomes this in done callbacks
argsOptionalArray of values passed as parameters
Return—Same Deferred (chainable)
Since—jQuery 1.5

Pseudocode

Pseudocode
resolveWith(context, argsArray):
    if already settled: return
    state = "resolved"
    remember context + argsArray
    for each done handler (add order):
        call handler with this = context
        and parameters = values from argsArray

resolve(a, b) ≈ resolveWith(defaultThis, [a, b])
// difference: you choose context with resolveWith

Cheat Sheet

Keep these facts handy when succeeding with a custom this.

TopicDetail
Syntaxdeferred.resolveWith(context [, args])
args shapeArray → spread as callback parameters
Triggersdone(), success branch of then(), always()
vs resolve()Same settle; resolveWith sets this
FamilyrejectWith, notifyWith
OnceSettles once; later settle calls ignored
SincejQuery 1.5

Live Preview

See how resolveWith() sets this and spreads an args array.

Browser demo of deferred.resolveWith — same idea as the examples below.

Result
this.id → uploader
result → complete

/* context becomes this in done() */

jQuery Examples

Short, copy-ready snippets. Open the browser console, or use Try It Yourself for a live editor.

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.
  4. Log together. Output combines object id and result text.

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.
  4. Spread happens. jQuery calls the handler with two separate args.

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.
  4. Args array. Remember the brackets: ["ok"], not a bare string as the second param.

Example 4 — Late done() After resolveWith()

Handlers added after resolution still run with the stored context and args.

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

dfd.resolveWith(ctx, ["finished"]);

dfd.done(function (result) {
  console.log(this.id + " → " + result);  // job-7 → finished
});
Try It Yourself

How It Works

  1. Resolve first. Context and args are stored; state is resolved.
  2. Attach done late. Official rule: it runs immediately.
  3. Same binding. this is still ctx.
  4. Same args. You still get "finished".

Example 5 — resolveWith Behind promise()

Only the owner resolves with context; callers listen on the Promise.

jQuery
function runTask(service, ok) {
  var dfd = $.Deferred();
  setTimeout(function () {
    if (ok) dfd.resolveWith(service, ["done"]);
    else dfd.rejectWith(service, ["failed"]);
  }, 200);
  return dfd.promise();
}

var svc = { name: "backup" };
runTask(svc, true).done(function (status) {
  console.log(this.name + ": " + status);  // backup: done
});
Try It Yourself

How It Works

  1. Private Deferred. Only runTask can settle.
  2. resolveWith(service, …). Done handlers see this === service.
  3. Return Promise. Callers cannot call resolveWith themselves.
  4. Listen with done. Logs backup: done.

Edge Cases & Pitfalls

Check these before using resolveWith() in real projects.

array

Second argument must be an array

Write resolveWith(ctx, ["ok"]), not resolveWith(ctx, "ok"). A bare string is not spread as one parameter the way you might expect.

arrow

Arrow functions ignore this

Arrow done handlers do not get the resolveWith context. Use a normal function when you need this.

once

Settles only once

After resolve/reject/*With, further settle calls are ignored.

owner

Keep settle methods private

Return promise() so callers can attach done but cannot call resolveWith.

Browser Support

deferred.resolveWith() needs jQuery 1.5+ loaded. It works across jQuery 1.5–3.x alongside resolve(), rejectWith(), and notifyWith().

✓ jQuery 1.5+

deferred.resolveWith()

Works in Chrome, Firefox, Safari, and Edge when jQuery 1.5+ is loaded. Use it when done handlers need a specific this.

100% With jQuery 1.5+
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
resolveWith() Universal

Bottom line: Safe wherever jQuery Deferreds exist. Prefer resolve() when you do not need a custom this.

Performance Notes

ApproachCostBest for
resolve(args…)Slightly simplerNo custom this
resolveWith(ctx, arr)Same order of costMethod-style done handlers
Huge args arraysCopied to handlersKeep payloads small

Prefer resolveWith for correct this binding, not for speed. The expensive part is usually your done handlers, not the settle call.

Key Takeaways

  • Role: resolve with a chosen this and an args array.
  • Args: second parameter is an array that spreads to done handlers.
  • Family: matches rejectWith / notifyWith.
  • Safety: owner-only; expose promise() to callers.

One line: resolveWith(ctx, [args…]) succeeds the Deferred and binds this for done handlers.

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

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

state() tutorial →

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