Created
February 4, 2016 20:40
-
-
Save dbarowy/3c237fd40f9dd46d3e46 to your computer and use it in GitHub Desktop.
Composed Scala futures with error-handling
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
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