Skip to content

Instantly share code, notes, and snippets.

@adamloving
Last active December 25, 2015 05:19
Show Gist options
  • Save adamloving/6923929 to your computer and use it in GitHub Desktop.
Save adamloving/6923929 to your computer and use it in GitHub Desktop.
Example for a friend on how to use jQuery Deferreds
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
@adamloving
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment