Created
May 5, 2015 22:36
-
-
Save pj/b3daace52dfa218cd444 to your computer and use it in GitHub Desktop.
Scala Future example
This file contains 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
Yep Futures/Promises are monads, in scala you'll often see code like: | |
val foo = for { | |
someResult <- doSomethingAsync(someArgument); | |
someOtherResult <- doAnotherThingAsync(someResult); | |
finalResult <- yetAnotherAsyncThing(someOtherResult); | |
} yield (finalResult) | |
foo.recover { | |
case SomeException e => "Something Went Wrong!" | |
} | |
Which is syntax sugar for: | |
val foo = doSomethingAsync.flatMap { | |
someResult => doSomeOtherThingAsync(someResult).flatMap { | |
someOtherResult => yetAnotherAsyncThing(someOtherResult) | |
} | |
} | |
foo.recover { | |
case SomeException e => "Something Went Wrong!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment