Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active August 29, 2015 14:15
Show Gist options
  • Save betandr/a28a5948a52137bf22ef to your computer and use it in GitHub Desktop.
Save betandr/a28a5948a52137bf22ef to your computer and use it in GitHub Desktop.
Serialized Futures using flatMap
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