Using a future directly for a simple operation
As an introductory example, we will see how to use a future just for a simple operation: we will add two integers and use a future to run this operation asynchronously.
Getting ready
To step through this recipe, we will need to import the Hello-Akka
project; all other prerequisites are the same as before.
How to do it...
For this recipe, we will need to perform the following steps:
- Create a file, such as
AddFuture.scala
, in thecom.packt.chapter4
package. - Add the following imports to the top of the file:
import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.concurrent.ExecutionContext.Implicits.global
- Create a test application, as follows:
object AddFuture extends App { val future = Future(1+2).mapTo[Int] val sum = Await.result(future, 10 seconds) println(s"Future Result $sum") }
How it works...
In the preceding recipe, we computed the sum of...