Mattia Micomonaco
Mattia Micomonaco

Reputation: 230

Sequential execution of a list of an arbitrary number of Scala Futures

I read about Scala Futures and Promises.

I know that I can use callbacks, the flatMap combinator or a for-comprehension to chain futures. When a future is completed, another future is started and so on.

Is there a way to chain the execution of an arbitrary number of futures, stored in a Scala Collection (for example a List)? I tried to invoke Future.sequence on this list of futures but they start all together and they are executed concurrently (not sequentially).

Upvotes: 0

Views: 1405

Answers (2)

Nabil A.
Nabil A.

Reputation: 3400

def chainFutures(list: List[() => Future[Any]]): Future[Any] = {
  list match {
    case x :: Nil => Future.successful(x)
    case (x :: xs) => x() flatMap (_ => chainFutures(xs))
  }
}

this should do what you want, if you can accept to provide a list of functions that return a Future. The problem with a List[Future[_]] is, that they are already started, so they cant be chained in terms of execution. Except you provide a single thread ExecutionContext.

In case of failure, it returns the first failure and stops executing the other futures.

Upvotes: 0

chengpohi
chengpohi

Reputation: 14217

My answer is more care about how to deal a list of arbitrary number of Scala Futures function.

  type FUTURE_FUNCTION = String => Future[String]

  def compose(fs: List[FUTURE_FUNCTION]): FUTURE_FUNCTION = fs match {
    case List(head) => head
    case head :: tail => head(_).flatMap { u => compose(tail)(u) }
  }

In the above code snippet, create a compose method and with high order Future function parameters. and this method iterate all high order functions and compose a full new Future function.

Example Use:

  val f1: String => Future[String] =
    (s: String) => Future {
      s.toUpperCase
    }


  val f2: String => Future[String] =
    (s: String) => Future {
      s + " Hello World"
    }

  compose(List(f1, f2))("foo bar")
  > FOO BAR Hello World

Upvotes: 4

Related Questions