Skip to content

Commit 74cdd78

Browse files
committed
deferred.promise(obj) should work with non-objects. Fixes #12521. Much needed unit tests added!
1 parent 9c5089a commit 74cdd78

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/deferred.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jQuery.extend({
4444
// Get a promise for this deferred
4545
// If obj is provided, the promise aspect is added to the object
4646
promise: function( obj ) {
47-
return typeof obj === "object" ? jQuery.extend( obj, promise ) : promise;
47+
return obj != null ? jQuery.extend( obj, promise ) : promise;
4848
}
4949
},
5050
deferred = {};

test/unit/deferred.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
88

99
test("jQuery.Deferred" + withNew, function() {
1010

11-
expect( 21 );
11+
expect( 23 );
1212

1313
var defer = createDeferred();
1414

@@ -39,6 +39,22 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
3939
strictEqual( value , "done" , "Passed function executed" );
4040
});
4141

42+
createDeferred(function( defer ) {
43+
var promise = defer.promise(),
44+
func = function() {},
45+
funcPromise = defer.promise( func );
46+
strictEqual( defer.promise(), promise, "promise is always the same" );
47+
strictEqual( funcPromise, func, "non objects get extended" );
48+
jQuery.each( promise, function( key, value ) {
49+
if ( !jQuery.isFunction( promise[ key ] ) ) {
50+
ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" );
51+
}
52+
if ( promise[ key ] !== func[ key ] ) {
53+
strictEqual( func[ key ], promise[ key ], key + " is the same" );
54+
}
55+
});
56+
});
57+
4258
jQuery.expandedEach = jQuery.each;
4359
jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) {
4460
createDeferred( function( defer ) {
@@ -59,6 +75,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
5975
});
6076
} );
6177

78+
6279
test( "jQuery.Deferred - chainability", function() {
6380

6481
var defer = jQuery.Deferred();

0 commit comments

Comments
 (0)