Getting data out of a Future in Scala

future, scala

Solution

There are multiple ways:

futPersons.map { personList =>
  ....
}

This map returns another `Future` composed with whatever you return from the map. The map will execute only if the future completes successfully. If you need to handle failure you can use onComplete

futPersons.onComplete {
  case Success(personList) => ...
  case Failure(exception)  =>  ... 
}

Or you can wait for the future to complete (this is blocking):

val personList: List[Person] = Await.result(futPersons, 1 minutes)

Problem

I've a `Future[List[Person]]`[1] and I want to get the `List[Person]` from it. How can I do it ? ``` import scala.concurrent.Future val futPersons : Future[List[Person]] = .... ```

Original source