Skip to content

Instantly share code, notes, and snippets.

@monokrome
Created April 19, 2014 22:59
Show Gist options
  • Select an option

  • Save monokrome/11100196 to your computer and use it in GitHub Desktop.

Select an option

Save monokrome/11100196 to your computer and use it in GitHub Desktop.
Example of a future
q = require 'q'
doIt = (accept=yes) ->
future = q.defer();
if accept
future.resolve 'accepted'
else
future.reject 'rejected'
# Return a promise for this future result
return future.promise
gonnaDoIt = doIt()
gonnaDoIt.then (whatDidIResolve) -> whatDidIResolve[0].toUpperCase() + whatDidIResolve[1..].toLowerCase()
gonnaDoIt.then (result) -> console.log result # will be 'Accepted'
notGonnaDoIt = doIt no
notGonnaDoIt.fail (why) -> console.log 'Failed due to: ' + why
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment