Skip to content

jQuery.when: Add Thenable and Promise as acceptable types #1026

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

Merged
merged 1 commit into from
Mar 20, 2017
Merged

jQuery.when: Add Thenable and Promise as acceptable types #1026

merged 1 commit into from
Mar 20, 2017

Conversation

Krinkle
Copy link
Member

@Krinkle Krinkle commented Mar 16, 2017

Technically replacing Deferred with Thenable would work, but mentioning Deferred and Promise directly should make this easier to understand for new and existing users.

Fixes #906.

@Krinkle
Copy link
Member Author

Krinkle commented Mar 16, 2017

This is a minimal change to unblock #983. A few other changes to consider separately:

  • Add an optional second parameter that is the same, to indicate that it takes variadic arguments, similar to append().
  • Add a secondary signature with Anything and improve documentation on the behaviour of wrapping non-thenable values.

pages/Types.html Outdated
@@ -657,6 +658,8 @@ <h2 id="XMLHttpRequest"> XMLHttpRequest </h2>
<h2 id="jqXHR"> jqXHR </h2>
<p>As of jQuery 1.5, the <a href="/jQuery.ajax/">$.ajax()</a> method returns the jqXHR object, which is a superset of the XMLHTTPRequest object. For more information, see the <a href="/jQuery.ajax/#jqXHR">jqXHR section of the $.ajax entry</a>
</p>
<h2 id="Thenable">Thenable</h2>
<p>Any object with a <code>then</code> method that returns a <a href="#Promise">Promise</a>-compatible object.</p>
Copy link
Member

Choose a reason for hiding this comment

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

We should stick to the definition at https://promisesaplus.com/#point-7 i.e. the only assumption about then is that it's a function.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe mentioning how it relates to promises somehow would clarify things but it should be obvious this is only an advisory remark and not part of the definition.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've simplified the phrasing, leaving out the Promise mention for now.

Technically replacing Deferred with Thenable would work, but
mentioning Deferred and Promise directly should make this
easier to understand for new and existing users.

Fixes #906.
@@ -657,6 +658,8 @@ <h2 id="XMLHttpRequest"> XMLHttpRequest </h2>
<h2 id="jqXHR"> jqXHR </h2>
<p>As of jQuery 1.5, the <a href="/jQuery.ajax/">$.ajax()</a> method returns the jqXHR object, which is a superset of the XMLHTTPRequest object. For more information, see the <a href="/jQuery.ajax/#jqXHR">jqXHR section of the $.ajax entry</a>
</p>
<h2 id="Thenable">Thenable</h2>
<p>Any object that has a <code>then</code> method.</p>
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, the spec at https://promisesaplus.com/#point-7 mentions functions with a then method as acceptable input as well.

That's a little weird one but technically we're still accepting those so maybe we should keep our definition identical to the Promises/A+ one?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mgol I considered that but found it more confusing than useful. All functions are objects. So this isn't contradictory.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, OK. I guess it was mentioned as that's the only object that has a non-"object" typeof.

@mgol mgol merged commit 3ab6720 into jquery:master Mar 20, 2017
@Krinkle Krinkle deleted the thenable branch March 20, 2017 22:37
@Krinkle Krinkle mentioned this pull request Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants