-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Split the two sides of jQuery.when #3059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
c74a03f to
2be1b05
Compare
|
src/deferred.js
Outdated
| resolveValues = slice.call( arguments ), | ||
| length = resolveValues.length, | ||
| when: function( singleValue ) { | ||
| var // count of uncompleted subordinates |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment position seems a little off-kilter here. Maybe add a newline or tab? Most likely a newline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed to match other instances.
|
Wow, that makes the code a lot easier to understand as well. 👍 |
src/deferred.js
Outdated
| var method; | ||
|
|
||
| // Check for promise aspect first to privilege synchronous behavior | ||
| if ( value && jQuery.isFunction( ( method = value.promise ) ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assignment inside of the call is attempt to save some size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it's not new. Saves 7 bytes here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the
function adoptValue( value, resolve, reject ) {
// Check for promise aspect first to privilege synchronous behavior
if ( value && jQuery.isFunction( value.promise ) ) {
value.promise.call( value ).done( resolve ).fail( reject );
// Other thenables
} else if ( value && jQuery.isFunction( value.then ) ) {
value.then.call( value, resolve, reject );
// Other non-thenables
} else {
// Support: Android 4.0 only
// Strict mode functions invoked without .call/.apply get global-object context
resolve.call( undefined, value );
}
}There is not diff in the size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Promises/A+ requires accessing .then only once; if ( jQuery.isFunction( value.then ) ) { value.then.call(…); } is prohibited (cf. Notes 3.5).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we don't fail any tests in this case then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markelog: Commenting it out won't fail any tests because Promises/A+ only specifies then, and we only define a promises-tests adapter for jQuery.Deferred. I can define a jQuery.when adapter, put in a QUnit test of jQuery.when( pathologicalThenable ), or just let it be, but the line MUST take that form if we guarantee proper treatment of any thenable conformant with Promises/A+ (which, as of 3.0, we do).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And to clarify: if ( jQuery.isFunction( value.then ) ) { value.then.call(…); } is prohibited because it accesses value.then twice, and the results of those accesses won't be identical in the presence of a pathological getter like https://github.com/promises-aplus/promises-tests/blob/0d409df278345753c9527539a5558db67d42bb02/lib/tests/helpers/thenables.js#L29 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or just let it be, but the line MUST take that form if we guarantee proper
I think such changes should be supported and confirmed by the tests, otherwise all of it could be seen as speculation or subjective interpretation, it seems this discussion revealed luck of it, so i'd say either in this pull or right after this one, we need to add those tests for jQuery.when.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single- and no-argument calls act like Promise.resolve. Multi-argument calls act like Promise.all. Fixes jquerygh-3029
f9fb6be to
be8174c
Compare
|
Rebased on the Android 4.0 fix. |
|
We're now taking a slight hit, but within tolerances. It would be nice to consolidate the |
|
|
||
| } ); | ||
| this.clock.tick( 50 ); | ||
| this.clock.tick( 100 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny you should ask... the test uses jQuery.when( elem ).done(…), and this PR updates the single-argument form of jQuery.when to use .then() for unwrapping secondary thenables (e.g., jQuery.when( jQuery.Deferred().resolve( thenable ) )). Single-argument jQuery.when becomes necessarily asynchronous as a side effect, and we therefore require an extra clock tick to invoke the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooo, so we have animate(..., 50), but because when.done is now async we need value more then 50, interesting, you probably were surprised a bit :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single-argument jQuery.when becomes necessarily asynchronous as a side effect
It feels to me like placing a distinction between single argument and multiple argument is a really bad idea. I opened #3100 about this.

Fixes gh-3029