Skip to content
Permalink
Browse files
deferred.promise(obj) should work with non-objects. Fixes #12521. Muc…
…h needed unit tests added!
  • Loading branch information
jaubourg committed Sep 13, 2012
1 parent 9c5089a commit 74cdd78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
@@ -44,7 +44,7 @@ jQuery.extend({
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function( obj ) {
return typeof obj === "object" ? jQuery.extend( obj, promise ) : promise;
return obj != null ? jQuery.extend( obj, promise ) : promise;

This comment has been minimized.

Copy link
@Krinkle

Krinkle Sep 21, 2012

Member

Could do with a comment making it explicit that it is intentional that all objects are supported (even objects that aren't typeof object), e.g. including function objects. Since it was a bugfix and is documented behavior.

Another common test is to verify that something is an object (including function objects) is Object( obj ) !== obj.

This comment has been minimized.

Copy link
@rwaldron

rwaldron Sep 21, 2012

Member

@Krinkle The funny thing about this: Object( obj ) !== obj is when you use jshint it will yell about not using new (which is optional), but when you add new it yells about making objects with new Object() vs {} (not optional) 😵

This comment has been minimized.

Copy link
@Krinkle

Krinkle Sep 21, 2012

Member

Not anymore it isn't :bowtie:

That's fixed now (jshint/jshint#392) as of JSHint r11 (2012-09-03). The native constructors are allowed to be invoked as functions to trigger the internal methods toNumber, toString, toObject etc. This was already the case for most of them, Object has joined that fix.

This comment has been minimized.

Copy link
@jaubourg

jaubourg Sep 21, 2012

Author Member

The unit test makes it clear which is what matters here: we will catch the regression now. That's why I always write the unit test first then try and fix the issue, btw.

This comment has been minimized.

Copy link
@rwaldron

rwaldron via email Sep 22, 2012

Member
}
},
deferred = {};
@@ -8,7 +8,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {

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

expect( 21 );
expect( 23 );

var defer = createDeferred();

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

createDeferred(function( defer ) {
var promise = defer.promise(),
func = function() {},
funcPromise = defer.promise( func );
strictEqual( defer.promise(), promise, "promise is always the same" );
strictEqual( funcPromise, func, "non objects get extended" );
jQuery.each( promise, function( key, value ) {
if ( !jQuery.isFunction( promise[ key ] ) ) {
ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" );
}
if ( promise[ key ] !== func[ key ] ) {
strictEqual( func[ key ], promise[ key ], key + " is the same" );
}
});
});

jQuery.expandedEach = jQuery.each;
jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) {
createDeferred( function( defer ) {
@@ -59,6 +75,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
});
} );


test( "jQuery.Deferred - chainability", function() {

var defer = jQuery.Deferred();

3 comments on commit 74cdd78

@jaubourg
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and this was done, tested and all on the iPad \o/

/me is mobile now :)

@rwaldron
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaubourg ooooo cloud9?

@jaubourg
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwldrn nope, I rooted the thing. Gotta try and install flash on it to be able to use browserstack btw.

Please sign in to comment.