Last active
August 29, 2015 14:15
-
-
Save betandr/a28a5948a52137bf22ef to your computer and use it in GitHub Desktop.
Serialized Futures using flatMap
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._ | |
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
val f1: Future[String] = Future { | |
Thread.sleep(200) | |
"done 1..." | |
} | |
val f2: Future[String] = Future { | |
Thread.sleep(200) | |
"done 2..." | |
} | |
val serializedFutures: Future[String] = f1 flatMap(res1 => f2) | |
f1 onComplete{ | |
case Success(result) => println(result) | |
case Failure(t) => println("An error has occured: " + t.getMessage) | |
} | |
f2 onComplete{ | |
case Success(result) => println(result) | |
case Failure(t) => println("An error has occured: " + t.getMessage) | |
} | |
serializedFutures onComplete{ | |
case resTry => println("Both Done: Success=" + resTry.isSuccess) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment