jQuery $.when() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

$.when() (same as jQuery.when()) takes one or more Deferreds, promises, or plain values and returns a single Promise that tracks them all (since jQuery 1.5). It resolves when every argument has resolved, and rejects as soon as any argument rejects.

Syntax

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

jQuery
$.when(deferred1, deferred2, ...)   → Promise

$.when(a, b).done(function (resA, resB) { ... });   // all resolved
$.when(a, b).fail(function (reason) { ... });       // first rejection
$.when(a, b).then(onOk, onFail);

$.when("value")    // non-Deferred: treated as resolved
$.when()           // no arguments: resolved immediately

// Typical use with Ajax:
$.when($.ajax("/page1.php"), $.ajax("/page2.php")).done(function (a1, a2) {
  // a1 and a2 are [data, textStatus, jqXHR] arrays
});
CallWhat it does
$.when(d1, d2, …)Promise that resolves when all arguments resolve
$.when(value)A non-Deferred argument is treated as an already-resolved Deferred
$.when()No arguments: returns a resolved promise at once
Any rejectionRejects immediately (fail-fast); other pending arguments are ignored
SincejQuery 1.5

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

Parameters

ParameterDescription
deferredsOne or more Deferred objects, promises (such as a jqXHR from $.ajax()), thenables like $.ready, or plain values.

Return Value

Return ValueDescription
PromiseA read-only Promise. done() receives one argument per input, in order; fail() receives the arguments of the first rejection.
Multiple valuesIf an input resolves with several arguments, its slot in done() is an array of those arguments.

jQuery Examples

Example 1 — Combine Two Deferreds

Run one callback after two separate Deferreds both resolve.

jQuery
var a = $.Deferred();
var b = $.Deferred();

$.when(a, b).done(function (x, y) {
  console.log(x + " + " + y);   // one + two
});

a.resolve("one");
b.resolve("two");
Try It Yourself

How It Works

  1. Create. Two pending Deferreds, a and b.
  2. Combine. $.when(a, b) returns a promise waiting on both.
  3. Resolve. After both resolve, done() gets their values in order.

Example 2 — Plain Values and Zero Arguments

Non-Deferred arguments count as resolved, and an empty call resolves immediately.

jQuery
$.when("hello", 42).done(function (s, n) {
  console.log(s + " " + n);       // hello 42
});

$.when("single").done(function (v) {
  console.log(v);                 // single
});

$.when().done(function () {
  console.log("resolved immediately");
});
Try It Yourself

How It Works

  1. Plain values. Strings and numbers are wrapped as resolved Deferreds.
  2. Single value. done() receives that one value directly.
  3. No arguments. $.when() returns a resolved promise at once.

Example 3 — Fail Fast on Reject

One rejection rejects the whole aggregate, even if another input is still pending.

jQuery
var a = $.Deferred();
var b = $.Deferred();

$.when(a, b)
  .done(function () { console.log("done"); })
  .fail(function (reason) { console.log("failed: " + reason); });

b.reject("b broke");
console.log("a is " + a.state());   // a is pending
Try It Yourself

How It Works

  1. Wait on both. The aggregate needs a and b to resolve.
  2. Reject one. b.reject() fails the aggregate immediately.
  3. Still pending. a never settled, yet fail() already ran.

Frequently Asked Questions

$.when() combines one or more Deferreds, promises, or plain values into a single Promise. It resolves when all inputs resolve and rejects as soon as any input rejects. Available since jQuery 1.5.
Pass each jqXHR: $.when($.ajax(url1), $.ajax(url2)).done(function (r1, r2) { ... }). Each r is an array of [data, textStatus, jqXHR] because $.ajax resolves with several arguments.
It is treated as an already-resolved Deferred, and its value is passed to done() in the matching position. With a single plain value, done() receives that value directly.
It returns a resolved promise immediately, so done() callbacks run right away with no arguments.
No. It is fail-fast: the first rejection rejects the aggregate and runs fail() callbacks. Other inputs are not cancelled, but their results are ignored by the aggregate.
Yes. In jQuery 3 $.ready is a thenable, so $.when($.ready, otherDeferred).done(...) waits for both the DOM and your async work.

Next: deferred.resolve() Method

Learn how to run a callback when a Deferred resolves successfully.

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