Skip to content

Instantly share code, notes, and snippets.

@dbarowy
Created February 4, 2016 20:40
Show Gist options
  • Save dbarowy/3c237fd40f9dd46d3e46 to your computer and use it in GitHub Desktop.
Save dbarowy/3c237fd40f9dd46d3e46 to your computer and use it in GitHub Desktop.
Composed Scala futures with error-handling
import scala.util.Random
import scala.concurrent.{ ExecutionContext, Future }
import ExecutionContext.Implicits.global
def okReverse(s: String) = s.reverse
def shittyReverse(s: String) = {
val r = new Random()
if (r.nextDouble >= 0.5) {
throw new Exception("sorry, not today")
} else {
okReverse(s)
}
}
def f1(s: String) = Future { shittyReverse(s) }
def f2(res1: Future[String]) =
res1 map okReverse recover {
case t: Throwable => t.getMessage()
} foreach println
f2(f1("my name is mud"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment