Created
August 27, 2012 18:01
-
-
Save bittersweetryan/3490886 to your computer and use it in GitHub Desktop.
jQuery Deferrds
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
var tweetClickHandler = function(e){ | |
var $that = $(this); | |
//while we are cleaning up the div go ahead and get | |
//the tweets | |
$.when( | |
getTweets($that.find('a').data('link')) | |
//since we care weather the dfd was resolved or rejected we use the .done method | |
//this only fires when the dfd is resolved | |
).done( | |
//each function we call in the when will pass its response | |
//to the then callback, in order | |
function(tweets){ | |
//now lets fade the div out and remove everything that was in it | |
//we could add add tweets in the clean div's fade out callback | |
//but this is more expressive in my opinion | |
$.when( | |
cleanDiv() | |
) | |
//then is called weather the dfd is resolved or rejected | |
.then( | |
function(){ | |
//again we want to only show the div after the tweets | |
//have been added | |
$.when( | |
addTweets(tweets) | |
) | |
.then( | |
function(){ | |
$tweetDiv.fadeIn('slow'); | |
} | |
) | |
} | |
) | |
} | |
) | |
//this is called when the deferred is rejected, in this | |
//case when there is an issue getting tweets | |
.fail( | |
function(text){ | |
alert("Error getting tweets: " + text || ''); | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment