12

In Scala 2.10, what is a correct way to write a function that returns a future which completes when all futures in a list complete?

After researching and experimenting, I have developed the code below, in a Scala Worksheet:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent._

object ws2 {

  def executeFutures(futures: Seq[Future[Unit]]): Future[Unit] = {

    def cascadeFutures(futureSeq: Seq[Future[Unit]], f: Future[Unit]): Future[Unit] = {
      futureSeq match {
        case h :: t => h.flatMap { u => cascadeFutures(t, f) }
        case nil => f
      }
    }

    cascadeFutures(futures, Future {})
  }                                               //> executeFutures: (futures: Seq[scala.concurrent.Future[Unit]])scala.concurren
                                                  //| t.Future[Unit]


  Await.ready(executeFutures(Seq(
    Future { println("Future1") },
    Future { println("Future2") },
    Future { println("Future3") }
  )) , 2.seconds)                                 //> Future1
                                                  //| Future2
                                                  //| Future3
                                                  //| res0: awaitable.type = scala.concurrent.impl.Promise$DefaultPromise@2dd063b3
                                                  //| 

}

I'm not sure that this code is correct, or, even if it's correct, if there is a better way.

It's not a problem if the futures are executed serially instead of in parallel.

This question is different from Wait for several Futures, which deals with a known number of futures.

Community
  • 1
  • 1
Fernando Correia
  • 21,803
  • 13
  • 83
  • 116

1 Answers1

13

Use Future.sequence to turn a List of Futures[T] into a single Future of List[T].

val listOfFutures:List[Future[T]] = ...
val futureOfList:Future[List[T]] = Future.sequence(listOfFutures)

This works, not just for Lists, but for any TraversableOnce, which includes most, if not all, of scala.collections.

ggovan
  • 1,907
  • 18
  • 21
  • 1
    Thank you for pointing out the `sequence` function. For dealing with `Future[Unit]`, I found out that `reduce` is also handy, like this: `Await.ready(Future.reduce(List(Future { println("Future1") },Future { println("Future2") },Future { println("Future3") }))((u1, u2) => u1), 1.seconds)` will execute all futures and return a `Future[Unit]`. – Fernando Correia Apr 30 '14 at 18:42