Last active
December 25, 2015 05:19
-
-
Save adamloving/6923929 to your computer and use it in GitHub Desktop.
Example for a friend on how to use jQuery Deferreds
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
doSomething: -> | |
d = jQuery.Deferred() | |
doTheWork: -> | |
data = hard() | |
d.resolve(data) | |
# return a promise which has a subset of the methods on a deferred | |
# a promise won't allow you to cancel the deferred for example | |
d.promise() | |
p = doSomething() # get the promise | |
# now setup the callbacks | |
p.done (data) -> | |
console.log 'Yay done!', data | |
p.fail -> | |
console.log 'uh oh!' | |
# example of waiting on multiple promises | |
$.when.apply(this, promises).done -> console.log 'all done', arguments | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A better explanation http://www.youtube.com/watch?v=HPb6e6EscEM