Last active
August 29, 2015 14:15
-
-
Save betandr/7893846319b2ba91c654 to your computer and use it in GitHub Desktop.
Serialized Futures using for comprehension
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..." | |
} | |
def combine(s1: String, s12: String): String = "result" | |
val serializedFutures = for { | |
val1 <- f1 | |
val2 <- f2 | |
} yield combine(val1, val2) | |
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