Tried asking this on stack overflow but could not get an answer. Trying here
Say I have the following snippet
def testFailure2() = {
val f1 = Future.failed(new Exception("ex1"))
val f2 = Future.successful(2);
val f3 = Future.successful((5));
val f4 = Future.failed(new Exception("ex4"))
val lst = List(f1, f2, f3, f4)
lst
}
The return type is List[Future[Int]]. In a normal way, I can just do Future.sequence and get List[Future[Int]]. But in this scenario it won’t work as I have a failed Future. So I want to convert this to List[Future[Int]] by ignoring the failed Futures. How do I do that?
Second Q on similar topic I have is, I understand filter, collect, partition, etc on a List. In this scenario, say I wanted to filter/partition the list into two lists - Failed Futures in one - Successfully done Futures in another.
That said, you may also want to look at a library that has more control over the execution model, like Monix. I haven’t used monix, but it looks like it would have a lot of flexibility if you are working with things like Futures a lot, and in more sophisticated ways.
I’d recommend Monix as well, but if you need to use Future then the recover and recoverWith methods let you essentially write a catch block for a Future, e.g.
import scala.util.control.NonFatal
def testFailure2() = {
val f1 = Future.failed(new Exception("ex1"))
val f2 = Future.successful(2);
val f3 = Future.successful((5));
val f4 = Future.failed(new Exception("ex4"))
val lst = List(f1, f2, f3, f4)
val handleErrors = lst.map(_.recover {
case NonFatal(e) => 0
})
Future.sequence(handleErrors)
}
lst
.map(_.map(Some(_)).recover { case _ => None }) // recover things
.foldLeft(Future.successful(List.empty[Int])) { // combine it together
case (accFuture, nextFuture) =>
nextFuture.zip(accFuture)
.map {
case (Some(next), acc) => next :: acc
case (_, acc) => acc
}
}