mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
356a3bccb0
Single- and no-argument calls act like Promise.resolve. Multi-argument calls act like Promise.all. Fixes gh-3029 Closes gh-3059
52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
/* jshint node: true */
|
|
|
|
"use strict";
|
|
|
|
require( "jsdom" ).env( "", function( errors, window ) {
|
|
if ( errors ) {
|
|
console.error( errors );
|
|
return;
|
|
}
|
|
|
|
var jQuery = require( ".." )( window );
|
|
|
|
exports.deferred = function() {
|
|
var adopted, promised,
|
|
obj = {
|
|
resolve: function() {
|
|
if ( !adopted ) {
|
|
adopted = jQuery.when.apply( jQuery, arguments );
|
|
if ( promised ) {
|
|
adopted.then( promised.resolve, promised.reject );
|
|
}
|
|
}
|
|
return adopted;
|
|
},
|
|
reject: function( value ) {
|
|
if ( !adopted ) {
|
|
adopted = jQuery.when( jQuery.Deferred().reject( value ) );
|
|
if ( promised ) {
|
|
adopted.then( promised.resolve, promised.reject );
|
|
}
|
|
}
|
|
return adopted;
|
|
},
|
|
|
|
// A manually-constructed thenable that works even if calls precede resolve/reject
|
|
promise: {
|
|
then: function() {
|
|
if ( !adopted ) {
|
|
if ( !promised ) {
|
|
promised = jQuery.Deferred();
|
|
}
|
|
return promised.then.apply( promised, arguments );
|
|
}
|
|
return adopted.then.apply( adopted, arguments );
|
|
}
|
|
}
|
|
};
|
|
|
|
return obj;
|
|
};
|
|
} );
|