Handling callback on futures
In this recipe, you will learn how to handle future responses with callback functions; it is also known as asynchronous handling of futures. Using the callback function, we don't block the current thread for response from a future.
Getting ready
To step through this recipe, we will need to import the Hello-Akka
project in the IDE, and the rest of the prerequisites are same as earlier.
How to do it...
- Create a scala file,
callback.scala
, in thecom.packt.chapter4
package. - Add the following import to the top of the file:
       import scala.concurrent.ExecutionContext.Implicits.global       import scala.concurrent.Future       import scala.util.{Failure, Success}
- Create a Scala application, as follows:
       object Callback extends App {       val future = Future(1 + 2).mapTo[Int]       future onComplete {       case Success(result) => println(s"result is $result")       case Failure(fail) => fail.printStackTrace()       ...