3 Deferred: function( func ) {
5 // action, add listener, listener list, final state
6 [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ],
7 [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ],
8 [ "notify", "progress", jQuery.Callbacks("memory") ]
16 deferred.done( arguments ).fail( arguments );
19 then: function( /* fnDone, fnFail, fnProgress */ ) {
21 return jQuery.Deferred(function( newDefer ) {
22 jQuery.each( tuples, function( i, tuple ) {
23 var action = tuple[ 0 ],
25 // deferred[ done | fail | progress ] for forwarding actions to newDefer
26 deferred[ tuple[1] ]( jQuery.isFunction( fn ) ?
28 var returned = fn.apply( this, arguments );
29 if ( returned && jQuery.isFunction( returned.promise ) ) {
31 .done( newDefer.resolve )
32 .fail( newDefer.reject )
33 .progress( newDefer.notify );
35 newDefer[ action + "With" ]( this === deferred ? newDefer : this, [ returned ] );
44 // Get a promise for this deferred
45 // If obj is provided, the promise aspect is added to the object
46 promise: function( obj ) {
47 return obj != null ? jQuery.extend( obj, promise ) : promise;
52 // Keep pipe for back-compat
53 promise.pipe = promise.then;
55 // Add list-specific methods
56 jQuery.each( tuples, function( i, tuple ) {
57 var list = tuple[ 2 ],
58 stateString = tuple[ 3 ];
60 // promise[ done | fail | progress ] = list.add
61 promise[ tuple[1] ] = list.add;
66 // state = [ resolved | rejected ]
69 // [ reject_list | resolve_list ].disable; progress_list.lock
70 }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
73 // deferred[ resolve | reject | notify ] = list.fire
74 deferred[ tuple[0] ] = list.fire;
75 deferred[ tuple[0] + "With" ] = list.fireWith;
78 // Make the deferred a promise
79 promise.promise( deferred );
81 // Call given func if any
83 func.call( deferred, deferred );
91 when: function( subordinate /* , ..., subordinateN */ ) {
93 resolveValues = core_slice.call( arguments ),
94 length = resolveValues.length,
96 // the count of uncompleted subordinates
97 remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
99 // the master Deferred. If resolveValues consist of only a single Deferred, just use that.
100 deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
102 // Update function for both resolve and progress values
103 updateFunc = function( i, contexts, values ) {
104 return function( value ) {
105 contexts[ i ] = this;
106 values[ i ] = arguments.length > 1 ? core_slice.call( arguments ) : value;
107 if( values === progressValues ) {
108 deferred.notifyWith( contexts, values );
109 } else if ( !( --remaining ) ) {
110 deferred.resolveWith( contexts, values );
115 progressValues, progressContexts, resolveContexts;
117 // add listeners to Deferred subordinates; treat others as resolved
119 progressValues = new Array( length );
120 progressContexts = new Array( length );
121 resolveContexts = new Array( length );
122 for ( ; i < length; i++ ) {
123 if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
124 resolveValues[ i ].promise()
125 .done( updateFunc( i, resolveContexts, resolveValues ) )
126 .fail( deferred.reject )
127 .progress( updateFunc( i, progressContexts, progressValues ) );
134 // if we're not waiting on anything, resolve the master
136 deferred.resolveWith( resolveContexts, resolveValues );
139 return deferred.promise();