1 import jQuery from "./core.js";
2 import slice from "./var/slice.js";
4 import "./callbacks.js";
6 function Identity( v ) {
9 function Thrower( ex ) {
13 function adoptValue( value, resolve, reject, noValue ) {
18 // Check for promise aspect first to privilege synchronous behavior
19 if ( value && typeof( method = value.promise ) === "function" ) {
20 method.call( value ).done( resolve ).fail( reject );
23 } else if ( value && typeof( method = value.then ) === "function" ) {
24 method.call( value, resolve, reject );
26 // Other non-thenables
29 // Control `resolve` arguments by letting Array#slice cast boolean `noValue` to integer:
30 // * false: [ value ].slice( 0 ) => resolve( value )
31 // * true: [ value ].slice( 1 ) => resolve()
32 resolve.apply( undefined, [ value ].slice( noValue ) );
35 // For Promises/A+, convert exceptions into rejections
36 // Since jQuery.when doesn't unwrap thenables, we can skip the extra checks appearing in
37 // Deferred#then to conditionally suppress rejection.
45 Deferred: function( func ) {
48 // action, add listener, callbacks,
49 // ... .then handlers, argument index, [final state]
50 [ "notify", "progress", jQuery.Callbacks( "memory" ),
51 jQuery.Callbacks( "memory" ), 2 ],
52 [ "resolve", "done", jQuery.Callbacks( "once memory" ),
53 jQuery.Callbacks( "once memory" ), 0, "resolved" ],
54 [ "reject", "fail", jQuery.Callbacks( "once memory" ),
55 jQuery.Callbacks( "once memory" ), 1, "rejected" ]
63 deferred.done( arguments ).fail( arguments );
66 catch: function( fn ) {
67 return promise.then( null, fn );
70 // Keep pipe for back-compat
71 pipe: function( /* fnDone, fnFail, fnProgress */ ) {
74 return jQuery.Deferred( function( newDefer ) {
75 jQuery.each( tuples, function( _i, tuple ) {
77 // Map tuples (progress, done, fail) to arguments (done, fail, progress)
78 var fn = typeof fns[ tuple[ 4 ] ] === "function" &&
81 // deferred.progress(function() { bind to newDefer or newDefer.notify })
82 // deferred.done(function() { bind to newDefer or newDefer.resolve })
83 // deferred.fail(function() { bind to newDefer or newDefer.reject })
84 deferred[ tuple[ 1 ] ]( function() {
85 var returned = fn && fn.apply( this, arguments );
86 if ( returned && typeof returned.promise === "function" ) {
88 .progress( newDefer.notify )
89 .done( newDefer.resolve )
90 .fail( newDefer.reject );
92 newDefer[ tuple[ 0 ] + "With" ](
94 fn ? [ returned ] : arguments
102 then: function( onFulfilled, onRejected, onProgress ) {
104 function resolve( depth, deferred, handler, special ) {
108 mightThrow = function() {
111 // Support: Promises/A+ section 2.3.3.3.3
112 // https://promisesaplus.com/#point-59
113 // Ignore double-resolution attempts
114 if ( depth < maxDepth ) {
118 returned = handler.apply( that, args );
120 // Support: Promises/A+ section 2.3.1
121 // https://promisesaplus.com/#point-48
122 if ( returned === deferred.promise() ) {
123 throw new TypeError( "Thenable self-resolution" );
126 // Support: Promises/A+ sections 2.3.3.1, 3.5
127 // https://promisesaplus.com/#point-54
128 // https://promisesaplus.com/#point-75
129 // Retrieve `then` only once
132 // Support: Promises/A+ section 2.3.4
133 // https://promisesaplus.com/#point-64
134 // Only check objects and functions for thenability
135 ( typeof returned === "object" ||
136 typeof returned === "function" ) &&
139 // Handle a returned thenable
140 if ( typeof then === "function" ) {
142 // Special processors (notify) just wait for resolution
146 resolve( maxDepth, deferred, Identity, special ),
147 resolve( maxDepth, deferred, Thrower, special )
150 // Normal processors (resolve) also hook into progress
153 // ...and disregard older resolution values
158 resolve( maxDepth, deferred, Identity, special ),
159 resolve( maxDepth, deferred, Thrower, special ),
160 resolve( maxDepth, deferred, Identity,
161 deferred.notifyWith )
165 // Handle all other returned values
168 // Only substitute handlers pass on context
169 // and multiple values (non-spec behavior)
170 if ( handler !== Identity ) {
175 // Process the value(s)
176 // Default process is resolve
177 ( special || deferred.resolveWith )( that, args );
181 // Only normal processors (resolve) catch and reject exceptions
189 if ( jQuery.Deferred.exceptionHook ) {
190 jQuery.Deferred.exceptionHook( e,
194 // Support: Promises/A+ section 2.3.3.3.4.1
195 // https://promisesaplus.com/#point-61
196 // Ignore post-resolution exceptions
197 if ( depth + 1 >= maxDepth ) {
199 // Only substitute handlers pass on context
200 // and multiple values (non-spec behavior)
201 if ( handler !== Thrower ) {
206 deferred.rejectWith( that, args );
211 // Support: Promises/A+ section 2.3.3.3.1
212 // https://promisesaplus.com/#point-57
213 // Re-resolve promises immediately to dodge false rejection from
219 // Call an optional hook to record the error, in case of exception
220 // since it's otherwise lost when execution goes async
221 if ( jQuery.Deferred.getErrorHook ) {
222 process.error = jQuery.Deferred.getErrorHook();
224 window.setTimeout( process );
229 return jQuery.Deferred( function( newDefer ) {
231 // progress_handlers.add( ... )
232 tuples[ 0 ][ 3 ].add(
236 typeof onProgress === "function" ?
243 // fulfilled_handlers.add( ... )
244 tuples[ 1 ][ 3 ].add(
248 typeof onFulfilled === "function" ?
254 // rejected_handlers.add( ... )
255 tuples[ 2 ][ 3 ].add(
259 typeof onRejected === "function" ?
267 // Get a promise for this deferred
268 // If obj is provided, the promise aspect is added to the object
269 promise: function( obj ) {
270 return obj != null ? jQuery.extend( obj, promise ) : promise;
275 // Add list-specific methods
276 jQuery.each( tuples, function( i, tuple ) {
277 var list = tuple[ 2 ],
278 stateString = tuple[ 5 ];
280 // promise.progress = list.add
281 // promise.done = list.add
282 // promise.fail = list.add
283 promise[ tuple[ 1 ] ] = list.add;
290 // state = "resolved" (i.e., fulfilled)
291 // state = "rejected"
295 // rejected_callbacks.disable
296 // fulfilled_callbacks.disable
297 tuples[ 3 - i ][ 2 ].disable,
299 // rejected_handlers.disable
300 // fulfilled_handlers.disable
301 tuples[ 3 - i ][ 3 ].disable,
303 // progress_callbacks.lock
304 tuples[ 0 ][ 2 ].lock,
306 // progress_handlers.lock
307 tuples[ 0 ][ 3 ].lock
311 // progress_handlers.fire
312 // fulfilled_handlers.fire
313 // rejected_handlers.fire
314 list.add( tuple[ 3 ].fire );
316 // deferred.notify = function() { deferred.notifyWith(...) }
317 // deferred.resolve = function() { deferred.resolveWith(...) }
318 // deferred.reject = function() { deferred.rejectWith(...) }
319 deferred[ tuple[ 0 ] ] = function() {
320 deferred[ tuple[ 0 ] + "With" ]( this === deferred ? undefined : this, arguments );
324 // deferred.notifyWith = list.fireWith
325 // deferred.resolveWith = list.fireWith
326 // deferred.rejectWith = list.fireWith
327 deferred[ tuple[ 0 ] + "With" ] = list.fireWith;
330 // Make the deferred a promise
331 promise.promise( deferred );
333 // Call given func if any
335 func.call( deferred, deferred );
343 when: function( singleValue ) {
346 // count of uncompleted subordinates
347 remaining = arguments.length,
349 // count of unprocessed arguments
352 // subordinate fulfillment data
353 resolveContexts = Array( i ),
354 resolveValues = slice.call( arguments ),
356 // the primary Deferred
357 primary = jQuery.Deferred(),
359 // subordinate callback factory
360 updateFunc = function( i ) {
361 return function( value ) {
362 resolveContexts[ i ] = this;
363 resolveValues[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
364 if ( !( --remaining ) ) {
365 primary.resolveWith( resolveContexts, resolveValues );
370 // Single- and empty arguments are adopted like Promise.resolve
371 if ( remaining <= 1 ) {
372 adoptValue( singleValue, primary.done( updateFunc( i ) ).resolve, primary.reject,
375 // Use .then() to unwrap secondary thenables (cf. gh-3000)
376 if ( primary.state() === "pending" ||
377 typeof( resolveValues[ i ] && resolveValues[ i ].then ) === "function" ) {
379 return primary.then();
383 // Multiple arguments are aggregated like Promise.all array elements
385 adoptValue( resolveValues[ i ], updateFunc( i ), primary.reject );
388 return primary.promise();
392 export default jQuery;