5 ], function( jQuery, slice ) {
9 function Identity( v ) {
12 function Thrower( ex ) {
16 function adoptValue( value, resolve, reject, noValue ) {
21 // Check for promise aspect first to privilege synchronous behavior
22 if ( value && jQuery.isFunction( ( method = value.promise ) ) ) {
23 method.call( value ).done( resolve ).fail( reject );
26 } else if ( value && jQuery.isFunction( ( method = value.then ) ) ) {
27 method.call( value, resolve, reject );
29 // Other non-thenables
32 // Control `resolve` arguments by letting Array#slice cast boolean `noValue` to integer:
33 // * false: [ value ].slice( 0 ) => resolve( value )
34 // * true: [ value ].slice( 1 ) => resolve()
35 resolve.apply( undefined, [ value ].slice( noValue ) );
38 // For Promises/A+, convert exceptions into rejections
39 // Since jQuery.when doesn't unwrap thenables, we can skip the extra checks appearing in
40 // Deferred#then to conditionally suppress rejection.
43 // Support: Android 4.0 only
44 // Strict mode functions invoked without .call/.apply get global-object context
45 reject.apply( undefined, [ value ] );
51 Deferred: function( func ) {
54 // action, add listener, callbacks,
55 // ... .then handlers, argument index, [final state]
56 [ "notify", "progress", jQuery.Callbacks( "memory" ),
57 jQuery.Callbacks( "memory" ), 2 ],
58 [ "resolve", "done", jQuery.Callbacks( "once memory" ),
59 jQuery.Callbacks( "once memory" ), 0, "resolved" ],
60 [ "reject", "fail", jQuery.Callbacks( "once memory" ),
61 jQuery.Callbacks( "once memory" ), 1, "rejected" ]
69 deferred.done( arguments ).fail( arguments );
72 "catch": function( fn ) {
73 return promise.then( null, fn );
76 // Keep pipe for back-compat
77 pipe: function( /* fnDone, fnFail, fnProgress */ ) {
80 return jQuery.Deferred( function( newDefer ) {
81 jQuery.each( tuples, function( i, tuple ) {
83 // Map tuples (progress, done, fail) to arguments (done, fail, progress)
84 var fn = jQuery.isFunction( fns[ tuple[ 4 ] ] ) && fns[ tuple[ 4 ] ];
86 // deferred.progress(function() { bind to newDefer or newDefer.notify })
87 // deferred.done(function() { bind to newDefer or newDefer.resolve })
88 // deferred.fail(function() { bind to newDefer or newDefer.reject })
89 deferred[ tuple[ 1 ] ]( function() {
90 var returned = fn && fn.apply( this, arguments );
91 if ( returned && jQuery.isFunction( returned.promise ) ) {
93 .progress( newDefer.notify )
94 .done( newDefer.resolve )
95 .fail( newDefer.reject );
97 newDefer[ tuple[ 0 ] + "With" ](
99 fn ? [ returned ] : arguments
107 then: function( onFulfilled, onRejected, onProgress ) {
109 function resolve( depth, deferred, handler, special ) {
113 mightThrow = function() {
116 // Support: Promises/A+ section 2.3.3.3.3
117 // https://promisesaplus.com/#point-59
118 // Ignore double-resolution attempts
119 if ( depth < maxDepth ) {
123 returned = handler.apply( that, args );
125 // Support: Promises/A+ section 2.3.1
126 // https://promisesaplus.com/#point-48
127 if ( returned === deferred.promise() ) {
128 throw new TypeError( "Thenable self-resolution" );
131 // Support: Promises/A+ sections 2.3.3.1, 3.5
132 // https://promisesaplus.com/#point-54
133 // https://promisesaplus.com/#point-75
134 // Retrieve `then` only once
137 // Support: Promises/A+ section 2.3.4
138 // https://promisesaplus.com/#point-64
139 // Only check objects and functions for thenability
140 ( typeof returned === "object" ||
141 typeof returned === "function" ) &&
144 // Handle a returned thenable
145 if ( jQuery.isFunction( then ) ) {
147 // Special processors (notify) just wait for resolution
151 resolve( maxDepth, deferred, Identity, special ),
152 resolve( maxDepth, deferred, Thrower, special )
155 // Normal processors (resolve) also hook into progress
158 // ...and disregard older resolution values
163 resolve( maxDepth, deferred, Identity, special ),
164 resolve( maxDepth, deferred, Thrower, special ),
165 resolve( maxDepth, deferred, Identity,
166 deferred.notifyWith )
170 // Handle all other returned values
173 // Only substitute handlers pass on context
174 // and multiple values (non-spec behavior)
175 if ( handler !== Identity ) {
180 // Process the value(s)
181 // Default process is resolve
182 ( special || deferred.resolveWith )( that, args );
186 // Only normal processors (resolve) catch and reject exceptions
194 if ( jQuery.Deferred.exceptionHook ) {
195 jQuery.Deferred.exceptionHook( e,
196 process.stackTrace );
199 // Support: Promises/A+ section 2.3.3.3.4.1
200 // https://promisesaplus.com/#point-61
201 // Ignore post-resolution exceptions
202 if ( depth + 1 >= maxDepth ) {
204 // Only substitute handlers pass on context
205 // and multiple values (non-spec behavior)
206 if ( handler !== Thrower ) {
211 deferred.rejectWith( that, args );
216 // Support: Promises/A+ section 2.3.3.3.1
217 // https://promisesaplus.com/#point-57
218 // Re-resolve promises immediately to dodge false rejection from
224 // Call an optional hook to record the stack, in case of exception
225 // since it's otherwise lost when execution goes async
226 if ( jQuery.Deferred.getStackHook ) {
227 process.stackTrace = jQuery.Deferred.getStackHook();
229 window.setTimeout( process );
234 return jQuery.Deferred( function( newDefer ) {
236 // progress_handlers.add( ... )
237 tuples[ 0 ][ 3 ].add(
241 jQuery.isFunction( onProgress ) ?
248 // fulfilled_handlers.add( ... )
249 tuples[ 1 ][ 3 ].add(
253 jQuery.isFunction( onFulfilled ) ?
259 // rejected_handlers.add( ... )
260 tuples[ 2 ][ 3 ].add(
264 jQuery.isFunction( onRejected ) ?
272 // Get a promise for this deferred
273 // If obj is provided, the promise aspect is added to the object
274 promise: function( obj ) {
275 return obj != null ? jQuery.extend( obj, promise ) : promise;
280 // Add list-specific methods
281 jQuery.each( tuples, function( i, tuple ) {
282 var list = tuple[ 2 ],
283 stateString = tuple[ 5 ];
285 // promise.progress = list.add
286 // promise.done = list.add
287 // promise.fail = list.add
288 promise[ tuple[ 1 ] ] = list.add;
295 // state = "resolved" (i.e., fulfilled)
296 // state = "rejected"
300 // rejected_callbacks.disable
301 // fulfilled_callbacks.disable
302 tuples[ 3 - i ][ 2 ].disable,
304 // progress_callbacks.lock
305 tuples[ 0 ][ 2 ].lock
309 // progress_handlers.fire
310 // fulfilled_handlers.fire
311 // rejected_handlers.fire
312 list.add( tuple[ 3 ].fire );
314 // deferred.notify = function() { deferred.notifyWith(...) }
315 // deferred.resolve = function() { deferred.resolveWith(...) }
316 // deferred.reject = function() { deferred.rejectWith(...) }
317 deferred[ tuple[ 0 ] ] = function() {
318 deferred[ tuple[ 0 ] + "With" ]( this === deferred ? undefined : this, arguments );
322 // deferred.notifyWith = list.fireWith
323 // deferred.resolveWith = list.fireWith
324 // deferred.rejectWith = list.fireWith
325 deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
328 // Make the deferred a promise
329 promise.promise( deferred );
331 // Call given func if any
333 func.call( deferred, deferred );
341 when: function( singleValue ) {
344 // count of uncompleted subordinates
345 remaining = arguments.length,
347 // count of unprocessed arguments
350 // subordinate fulfillment data
351 resolveContexts = Array( i ),
352 resolveValues = slice.call( arguments ),
354 // the master Deferred
355 master = jQuery.Deferred(),
357 // subordinate callback factory
358 updateFunc = function( i ) {
359 return function( value ) {
360 resolveContexts[ i ] = this;
361 resolveValues[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
362 if ( !( --remaining ) ) {
363 master.resolveWith( resolveContexts, resolveValues );
368 // Single- and empty arguments are adopted like Promise.resolve
369 if ( remaining <= 1 ) {
370 adoptValue( singleValue, master.done( updateFunc( i ) ).resolve, master.reject,
373 // Use .then() to unwrap secondary thenables (cf. gh-3000)
374 if ( master.state() === "pending" ||
375 jQuery.isFunction( resolveValues[ i ] && resolveValues[ i ].then ) ) {
377 return master.then();
381 // Multiple arguments are aggregated like Promise.all array elements
383 adoptValue( resolveValues[ i ], updateFunc( i ), master.reject );
386 return master.promise();