Build: Add `exports` to package.json, export slim & esm builds
[jquery.git] / test / promises_aplus_adapters / when.cjs
blob3e945d47558e1991d2036ad0232cdb20c77b76de
1 "use strict";
3 const { JSDOM } = require( "jsdom" );
5 const { window } = new JSDOM( "" );
7 const jQuery = require( "../../" )( window );
9 module.exports.deferred = () => {
10         let adopted, promised;
12         return {
13                 resolve: function() {
14                         if ( !adopted ) {
15                                 adopted = jQuery.when.apply( jQuery, arguments );
16                                 if ( promised ) {
17                                         adopted.then( promised.resolve, promised.reject );
18                                 }
19                         }
20                         return adopted;
21                 },
22                 reject: function( value ) {
23                         if ( !adopted ) {
24                                 adopted = jQuery.when( jQuery.Deferred().reject( value ) );
25                                 if ( promised ) {
26                                         adopted.then( promised.resolve, promised.reject );
27                                 }
28                         }
29                         return adopted;
30                 },
32                 // A manually-constructed thenable that works even if calls precede resolve/reject
33                 promise: {
34                         then: function() {
35                                 if ( !adopted ) {
36                                         if ( !promised ) {
37                                                 promised = jQuery.Deferred();
38                                         }
39                                         return promised.then.apply( promised, arguments );
40                                 }
41                                 return adopted.then.apply( adopted, arguments );
42                         }
43                 }
44         };