Skip to content

Instantly share code, notes, and snippets.

@pj
Created May 5, 2015 22:36
Show Gist options
  • Save pj/b3daace52dfa218cd444 to your computer and use it in GitHub Desktop.
Save pj/b3daace52dfa218cd444 to your computer and use it in GitHub Desktop.
Scala Future example
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