September 2018
Intermediate to advanced
398 pages
9h 43m
English
Another way to get the result is to use a callback function. In this case, we stay asynchronous to get the value. The syntax is as follows:
Import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
val f: Future[String] = Future {
Thread.sleep(1000)
“Finished”
}
f.onComplete {
case Success(value) => println(value)
case Failure(e) => e.printStackTrace()
}
First, Future is created and assigned to f; then, we handle the success case, and then, the failure case.
Read now
Unlock full access