Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betandr/7893846319b2ba91c654 to your computer and use it in GitHub Desktop.
Save betandr/7893846319b2ba91c654 to your computer and use it in GitHub Desktop.
Serialized Futures using for comprehension
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