$.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
});
Call
What 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 rejection
Rejects immediately (fail-fast); other pending arguments are ignored
Since
jQuery 1.5
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
deferreds
One or more Deferred objects, promises (such as a jqXHR from $.ajax()), thenables like $.ready, or plain values.
Return Value
Return Value
Description
Promise
A read-only Promise. done() receives one argument per input, in order; fail() receives the arguments of the first rejection.
Multiple values
If 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");
Plain values. Strings and numbers are wrapped as resolved Deferreds.
Single value.done() receives that one value directly.
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
Wait on both. The aggregate needs a and b to resolve.
Reject one.b.reject() fails the aggregate immediately.
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.