Skip to content

Commit 987b2da

Browse files
committed
jQuery Deferreds: Improve last example and provide more detailed explanation
1 parent 493807e commit 987b2da

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

page/code-organization/deferreds/jquery-deferreds.md

+21-10
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ $.when(
3737
).then( successFunc, failureFunc );
3838
```
3939

40-
The `$.when()` implementation offered in jQuery is quite interesting as it not only interprets deferred objects, but when passed arguments that are not deferreds, it treats these as if they were resolved deferreds and executes any callbacks (doneCallbacks) right away. It is also worth noting that jQuery's deferred implementation, in addition to exposing deferred.then(), a jQuery promise also supports the deferred.done() and deferred.fail() methods which can also be used to add callbacks to the deferred's queues.
40+
The `$.when()` implementation offered in jQuery is quite interesting as it not only interprets deferred objects, but when passed arguments that are not deferreds, it treats these as if they were resolved deferreds and executes any callbacks (`doneCallbacks`) right away. It is also worth noting that jQuery's deferred implementation, in addition to exposing `deferred.then()`, also supports the `deferred.done()` and `deferred.fail()` methods which can also be used to add callbacks to the deferred's queues.
4141

42-
We will now take a look at a code example that utilizes many of the deferred features mentioned in the table presented earlier. Here is a very basic application that consumes (1) an external news feed and (2) a reactions feed for pulling in the latest comments via `$.get()` (which will return a promise). The application also has a function (`prepareInterface()`) which returns a promise to complete animating our containers for both the news and reactions.
42+
We will now take a look at a code example that uses many deferred features. This very basic script begins by consuming (1) an external news feed and (2) a reactions feed for pulling in the latest comments via `$.get()` (which will return a promise-like object). When both requests are received, the `showAjaxedContent()` function is called. The `showAjaxedContent()` function returns a promise that is resolved when animating both containers has completed. When the `showAjaxedContent()` promise is resolved, `removeActiveClass()` is called. The `removeActiveClass()` returns a promise that is resolved inside a `setTimeout()` after 4 seconds have elapsed. Finally, after the `removeActiveClass()` promise is resolved, the last `then()` callback is called, provided no errors occurred along the way.
4343

4444
```
4545
function getLatestNews() {
@@ -56,20 +56,31 @@ function getLatestReactions() {
5656
});
5757
}
5858
59-
function prepareInterface() {
59+
function showAjaxedContent() {
60+
// The .promise() is resolved *once*, after all animations complete
61+
return $( ".news, .reactions" ).slideDown( 500, function() {
62+
// Called once *for each element* when animation completes
63+
$(this).addClass( "active" );
64+
}).promise();
65+
}
66+
67+
function removeActiveClass() {
6068
return $.Deferred(function( dfd ) {
61-
var latest = $( ".news, .reactions" );
62-
latest.slideDown( 500, dfd.resolve );
63-
latest.addClass( "active" );
69+
setTimeout(function () {
70+
$( ".news, .reactions" ).removeClass( "active" );
71+
dfd.resolve();
72+
}, 4000);
6473
}).promise();
6574
}
6675
6776
$.when(
6877
getLatestNews(),
69-
getLatestReactions(),
70-
prepareInterface()
71-
).then(function() {
72-
console.log( "fire after requests succeed" );
78+
getLatestReactions()
79+
)
80+
.then(showAjaxedContent)
81+
.then(removeActiveClass)
82+
.then(function() {
83+
console.log( "Requests succeeded and animations completed" );
7384
}).fail(function() {
7485
console.log( "something went wrong!" );
7586
});

0 commit comments

Comments
 (0)