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]] = ....
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)
Blocking way (pauses your thread until you get the value back) using Await.result
:
scala.concurrent.Await.result(futPersons, timeout)
Or, using a callback with onSuccess
:
futPersons onSuccess {
case persons => // do something with persons
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With