-
Notifications
You must be signed in to change notification settings - Fork 266
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
jQuery.when: Improved example #915
Open
AurelioDeRosa
wants to merge
1
commit into
jquery:main
Choose a base branch
from
AurelioDeRosa:when
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,15 @@ | |
<p>If no arguments are passed to <code>jQuery.when()</code>, it will return a resolved Promise.</p> | ||
<p>If a single Deferred is passed to <code>jQuery.when()</code>, its Promise object (a subset of the Deferred methods) is returned by the method. Additional methods of the Promise object can be called to attach callbacks, such as <a href="/deferred.then/"><code>deferred.then</code></a>. When the Deferred is resolved or rejected, usually by the code that created the Deferred originally, the appropriate callbacks will be called. For example, the jqXHR object returned by <code>jQuery.ajax()</code> is a Promise-compatible object and can be used this way:</p> | ||
<pre><code> | ||
$.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) { | ||
alert( jqXHR.status ); // Alerts 200 | ||
$.when( $.ajax( "test.aspx" ), $.ajax( "page.aspx" ) ).then(function( res1, res2 ) { | ||
alert( res1[2].status ); // Alerts 200 | ||
}) | ||
.fail(function( err1, err2 ) { | ||
alert( err1.status ); | ||
}); | ||
</code></pre> | ||
<p>In the previous example, <code>res1</code> and <code>res2</code> are arrays containing three values: <code>data</code>, <code>textStatus</code>, and <code>jqXHR</code>. It is adviced to always provide a callback to handle errors through the use of <code>deferred.fail()</code> or the second parameter of <code>deferred.then()</code>.</p> | ||
<p>When a single <a href="/Types/#Deferred">Deferred</a>, <a href="/Types/#Promise">Promise</a>, or Promise-compatible object is passed to <code>jQuery.when()</code>, an argument for each of the returned value is provided to the <code>doneCallback</code> instead of a single argument containing an array of values. It is suggested not to rely on this behavior because it is considered an anti-pattern and it will change in the upcoming versions of jQuery, starting with version 3.</p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is worded a little too strongly. What we really have are two distinct signatures (single-argument and multi-argument), each with distinct behavior ("cast"/"get as deferred"/etc. and "join", respectively) that will be well-tested as of jQuery 3.0. |
||
<p>If a single argument is passed to <code>jQuery.when()</code> and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately. The doneCallbacks are passed the original argument. In this case any failCallbacks you might set are never called since the Deferred is never rejected. For example:</p> | ||
<pre><code> | ||
$.when( { testing: 123 } ).done(function( x ) { | ||
|
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.
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.
Should we switch to
.catch()
here @gibson042? Also since the failure occurs as soon as any rejection occurs, I think it's possible thatres1
does not yet have a status ifres2
rejected. If a programming exception occurred they might even beError
objects instead ofjQXHR
objects! In real code you'd need to inspect both to sort it out.Perhaps for the purposes of this example we could just
alert("one or more requests failed")
.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.
Your instincts are good, @dmethvin. I agree on all counts.