carrotcake
carrotcake

Reputation: 1

How to run a sequence of futures without blocking and wait until completion?

I have a requirement in Scala to run a series of http calls that have to be completed in sequence and without blocking. How can I achieve that?

Upvotes: 0

Views: 203

Answers (1)

peterschrott
peterschrott

Reputation: 582

You want to have a look at the foldLeft function or your TraversableLike containing the series of calls or their specs:

val seriesOfOrderedCalls = Seq(..)
val eventuallyCompleted = seriesOfOrderedCalls
  .foldLeft(Future.successful(()))((prev, call) => {
    prev.flatMap { _ =>
      // do your call here
      // then return the future of the call
      Future.successful(())
    }
  })

Upvotes: 1

Related Questions