concept Future in category scala

appears as: Future, Future
Scala in Action

This is an excerpt from Manning's book Scala in Action.

In this case someFuture will hold the result of the computation and T represents the type of the result. Since the Future is executed asynchronously we need to specify the scala.concurrent.ExecutionContext. ExecutionContext is an abstraction over a thread pool that is responsible for executing all the tasks submitted to it. Here the task is the computation performed by the Future. There are many ways to configure and create ExecutionContext but in this chapter we will use the default global execution context available in the Scala library.

import ExecutionContext.Implicits.global

When the Future has the value it is considered completed. A Future could also be completed with an exception. To do an operation after the Future is completed we can use the onComplete callback method as in following:

someFuture.onComplete {
  case Success(result) => println(result)
  case Failure(t) => t.printStackTrace
}

Since a Future could be a success or failed state, the onComplete allows you to handle both conditions. (Check the scala.concurrent.Future scaladoc for more details.)