Search code examples
scalafuture

Scala: List[Future] to Future[List] disregarding failed futures


I'm looking for a way to convert an arbitrary length list of Futures to a Future of List. I'm using Playframework, so ultimately, what I really want is a Future[Result], but to make things simpler, let's just say Future[List[Int]] The normal way to do this would be to use Future.sequence(...) but there's a twist... The list I'm given usually has around 10-20 futures in it, and it's not uncommon for one of those futures to fail (they are making external web service requests).

Instead of having to retry all of them in the event that one of them fails, I'd like to be able to get at the ones that succeeded and return those.

For example, doing the following doesn't work:

import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Success
import scala.util.Failure

val listOfFutures = Future.successful(1) :: Future.failed(new Exception("Failure")) :: 
                    Future.successful(3) :: Nil

val futureOfList = Future.sequence(listOfFutures)

futureOfList onComplete {
  case Success(x) => println("Success!!! " + x)
  case Failure(ex) => println("Failed !!! " + ex)
}

scala> Failed !!! java.lang.Exception: Failure

Instead of getting the only the exception, I'd like to be able to pull the 1 and 3 out of there. I tried using Future.fold, but that apparently just calls Future.sequence behind the scenes.


Solution

  • The trick is to first make sure that none of the futures has failed. .recover is your friend here, you can combine it with map to convert all the Future[T] results to Future[Try[T]]] instances, all of which are certain to be successful futures.

    note: You can use Option or Either as well here, but Try is the cleanest way if you specifically want to trap exceptions

    def futureToFutureTry[T](f: Future[T]): Future[Try[T]] =
      f.map(Success(_)).recover { case x => Failure(x)}
    
    val listOfFutures = ...
    val listOfFutureTrys = listOfFutures.map(futureToFutureTry(_))
    

    Then use Future.sequence as before, to give you a Future[List[Try[T]]]

    val futureListOfTrys = Future.sequence(listOfFutureTrys)
    

    Then filter:

    val futureListOfSuccesses = futureListOfTrys.map(_.filter(_.isSuccess))
    

    You can even pull out the specific failures, if you need them:

    val futureListOfFailures = futureListOfTrys.map(_.filter(_.isFailure))