Deferred: Preserve synchronous resolution in jQuery.when for Deferreds#5784
Open
Nicknamess96 wants to merge 1 commit into
Open
Deferred: Preserve synchronous resolution in jQuery.when for Deferreds#5784Nicknamess96 wants to merge 1 commit into
Nicknamess96 wants to merge 1 commit into
Conversation
When jQuery.when() is called with a single pending jQuery Deferred, pipe through a subordinate Deferred using .done()/.fail() instead of .then() to avoid unnecessary asynchronous scheduling via setTimeout. The existing .then() path is still used for non-jQuery thenables (e.g., native Promises) which require async unwrapping. Secondary thenable unwrapping is preserved by re-adopting the resolved value through adoptValue on the subordinate. Fixes jquerygh-4798 Closes jquerygh-4798
|
|
Author
|
Friendly ping — this fixes a regression where jQuery.when doesn't preserve synchronous resolution for Deferreds. Tests pass. Happy to address any feedback. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
jQuery.when()is called with a single pending jQuery Deferred, the returnedpromise now resolves synchronously (via
.done()/.fail()) instead of beingscheduled asynchronously via
setTimeoutthrough.then().The Problem
As reported in #4798,
jQuery.when(deferred)unnecessarily introduces asynchronousbehavior when the single argument is a jQuery Deferred:
This happens because
jQuery.whenreturnsprimary.then()for pending single-valuecases, and
.then()schedules handlers viawindow.setTimeoutper the Promises/A+spec. However, jQuery Deferreds use
.done()/.fail()for synchronous notification,so this async scheduling is unnecessary when the argument is a Deferred.
The Fix
When the single argument has a
.promise()method (i.e., is a jQuery Deferred),pipe resolution through a subordinate Deferred using
.done()/.fail()insteadof
.then(). The resolved value is re-adopted viaadoptValueto preservesecondary thenable unwrapping (cf. gh-3000).
Non-jQuery thenables (native Promises, custom thenables) still use the
.then()path, preserving their expected async behavior.
Tests
Added a new test case
"jQuery.when(pendingDeferred) - synchronous resolution (gh-4798)"that verifies:.done()handler fires synchronously on resolve.fail()handler fires synchronously on rejectAll existing tests pass, including the Promises/A+ compliance suite (872/872).
Fixes gh-4798