|
3 | 3 | <title>jQuery.when()</title>
|
4 | 4 | <signature>
|
5 | 5 | <added>1.5</added>
|
6 |
| - <argument name="deferreds" type="Deferred"> |
7 |
| - <desc>One or more Deferred objects, or plain JavaScript objects.</desc> |
| 6 | + <argument name="value" type="Anything" optional="true"> |
| 7 | + <desc>A Promise-like object to resolve a Deferred object, or any value to encapsulate within a Deferred object.</desc> |
| 8 | + </argument> |
| 9 | + </signature> |
| 10 | + <signature> |
| 11 | + <added>1.5</added> |
| 12 | + <argument name="values" type="Anything"> |
| 13 | + <desc>Two or values to resolve or encapsulate with Deferred objects for <code>Promise.all</code>-like fulfillment aggregation.</desc> |
8 | 14 | </argument>
|
9 | 15 | </signature>
|
10 | 16 | <desc>Provides a way to execute callback functions based on one or more objects, usually <a href="/category/deferred-object/">Deferred</a> objects that represent asynchronous events.</desc>
|
11 | 17 | <longdesc>
|
12 |
| - <p>If a single Deferred is passed to <code>jQuery.when()</code>, its Promise object (a subset of the Deferred methods) is returned by the method. Additional methods of the Promise object can be called to attach callbacks, such as <a href="/deferred.then/"><code>deferred.then</code></a>. When the Deferred is resolved or rejected, usually by the code that created the Deferred originally, the appropriate callbacks will be called. For example, the jqXHR object returned by <code>jQuery.ajax()</code> is a Promise-compatible object and can be used this way:</p> |
| 18 | + <p>If no arguments are provided, <code>jQuery.when()</code> will return a resolved Promise.</p> |
| 19 | + <pre><code> |
| 20 | +$.when().then(function( x ) { |
| 21 | + alert( "I fired immediately" ); |
| 22 | +}); |
| 23 | + </code></pre> |
| 24 | + <p>If a single argument is provided and it is a Promise or has a <code>then</code> method, <code>jQuery.when()</code> will return a Promise that will resolve or reject identically to the argument.</p> |
13 | 25 | <pre><code>
|
14 | 26 | $.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) {
|
15 | 27 | alert( jqXHR.status ); // Alerts 200
|
16 | 28 | });
|
17 | 29 | </code></pre>
|
18 |
| - <p>If a single argument is passed to <code>jQuery.when()</code> and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. The doneCallbacks are passed the original argument. In this case any failCallbacks you might set are never called since the Deferred is never rejected. For example:</p> |
| 30 | + <p>If any other single argument is provided, <code>jQuery.when()</code> will return a Promise that is resolved to it.</p> |
19 | 31 | <pre><code>
|
20 | 32 | $.when( { testing: 123 } ).done(function( x ) {
|
21 | 33 | alert( x.testing ); // Alerts "123"
|
22 | 34 | });
|
23 | 35 | </code></pre>
|
24 |
| - <p>If you don't pass it any arguments at all, <code>jQuery.when()</code> will return a resolved promise.</p> |
25 |
| - <pre><code> |
26 |
| -$.when().then(function( x ) { |
27 |
| - alert( "I fired immediately" ); |
28 |
| -}); |
29 |
| - </code></pre> |
30 |
| - <p>In the case where multiple Deferred objects are passed to <code>jQuery.when()</code>, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, the doneCallbacks for the master Deferred are executed. The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to <code>jQuery.when()</code>. For example:</p> |
| 36 | + <p>If more than one argument is provided, <code>jQuery.when()</code> will return the Promise from a new "master" Deferred object that tracks their aggregate state (upgrading each to a Deferred object as in the single-argument case where necessary). The master Deferred will resolve as soon as all subordinates resolve, or reject as soon as one is rejected. If the master Deferred is resolved, the resolution context and values of each subordinate are preserved in its resolution context (an array of all the resolution contexts) and resolution values (the resolution values of each subordinate appear as a single value, with multi-valued resolutions theirselves becoming arrays), each preserving the order in which the subordinate was provided to <code>jQuery.when()</code>. For example:</p> |
31 | 37 | <pre><code>
|
32 | 38 | var d1 = $.Deferred();
|
33 | 39 | var d2 = $.Deferred();
|
| 40 | +var d3 = $.Deferred(); |
34 | 41 |
|
35 |
| -$.when( d1, d2 ).done(function ( v1, v2 ) { |
36 |
| - console.log( v1 ); // "Fish" |
37 |
| - console.log( v2 ); // "Pizza" |
| 42 | +$.when( d1, d2 ).done(function ( v1, v2, v3 ) { |
| 43 | + console.log( v1 ); // "Fish" |
| 44 | + console.log( v2 ); // ["Pizza", "Pie"] |
| 45 | + console.log( v3 ); // undefined |
| 46 | + console.log( this ); // [{ where: "Ocean" }, undefined, undefined] |
38 | 47 | });
|
39 | 48 |
|
40 |
| -d1.resolve( "Fish" ); |
41 |
| -d2.resolve( "Pizza" ); |
| 49 | +d1.resolveWith( { where: "Ocean" }, [ "Fish" ] ); |
| 50 | +d2.resolve( "Pizza", "Pie" ); |
| 51 | +d3.resolve(); |
42 | 52 | </code></pre>
|
43 |
| - <p>In the event a Deferred was resolved with no value, the corresponding doneCallback argument will be undefined. If a Deferred resolved to a single value, the corresponding argument will hold that value. In the case where a Deferred resolved to multiple values, the corresponding argument will be an array of those values. For example:</p> |
| 53 | + <p>If more than one argument is provided and one of the arguments is rejected, <code>jQuery.when()</code> immediately rejects with corresponding context and values. Note that some of the subordinates may still be unresolved at that point. For example:</p> |
44 | 54 | <pre><code>
|
45 | 55 | var d1 = $.Deferred();
|
46 | 56 | var d2 = $.Deferred();
|
47 | 57 | var d3 = $.Deferred();
|
48 | 58 |
|
49 |
| -$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) { |
50 |
| - console.log( v1 ); // v1 is undefined |
51 |
| - console.log( v2 ); // v2 is "abc" |
52 |
| - console.log( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ] |
| 59 | +$.when( d1, d2 ).fail(function ( rejection ) { |
| 60 | + console.log( rejection ); // "Pie" |
| 61 | + console.log( this ); // { type: "Pizza" } |
53 | 62 | });
|
54 | 63 |
|
55 |
| -d1.resolve(); |
56 |
| -d2.resolve( "abc" ); |
57 |
| -d3.resolve( 1, 2, 3, 4, 5 ); |
| 64 | +d1.resolve( "Fish" ); |
| 65 | +d2.rejectWith( { type: "Pizza" }, [ "Pie" ] ); |
| 66 | +d3.resolve(); |
58 | 67 | </code></pre>
|
59 |
| - <p>In the multiple-Deferreds case where one of the Deferreds is rejected, <code>jQuery.when()</code> immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. The arguments passed to the failCallbacks match the signature of the failCallback for the Deferred that was rejected. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.</p> |
| 68 | + <p>Regardless of provided arguments, methods of the returned Promise object can be called to attach callbacks, such as <a href="/deferred.then/"><code>deferred.then</code></a>. When the Deferred is resolved or rejected, usually by the code that created the Deferred originally, the appropriate callbacks will be called.</p> |
60 | 69 | </longdesc>
|
61 | 70 | <example>
|
62 | 71 | <desc>Execute a function after two ajax requests are successful. (See the jQuery.ajax() documentation for a complete description of success and error cases for an ajax request).</desc>
|
|
0 commit comments