Created
April 19, 2014 22:59
-
-
Save monokrome/11100196 to your computer and use it in GitHub Desktop.
Example of a future
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
| 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