What is the pattern used in Scala to deal with the scenario:
You have a bunch of futures (they can be whatever, but for the sake of example...)
val aF = Future { true }
val bF = Future { Option(3) }
val cF = Future { myObject }
and you have some function that returns a future
def fooF: Future[SomeObject]
I want to do something like:
for {
a <- aF
b <- bF
c <- cF
} yield {
if (a) {
// do stuff with b & c
fooF
} else {
Future.successful(SomeObject)
}
}
I want to return a value of Future[SomeObject], but I call fooF inside of the yield statement, I will get a Future[Future[SomeObject]]
<-before yield. Try to refactor with nested flatMap to understand. - cchantepFuture[SomeObject]aslazy valordef, but it will also delay all the other futures in the computation (unless they were started beforehand). - Cyrille Corpet