Skip to content

Instantly share code, notes, and snippets.

@matterche
Created July 20, 2015 15:20
Show Gist options
  • Save matterche/42191bb696a5a64bc4e2 to your computer and use it in GitHub Desktop.
Save matterche/42191bb696a5a64bc4e2 to your computer and use it in GitHub Desktop.
Non-blocking serial execution of futures
// see http://www.michaelpollmeier.com/execute-scala-futures-in-serial-one-after-the-other-non-blocking/
def serialiseFutures[A, B](l: Iterable[A])(fn: A ⇒ Future[B])
(implicit ec: ExecutionContext): Future[List[B]] =
l.foldLeft(Future(List.empty[B])) {
(previousFuture, next) ⇒
for {
previousResults ← previousFuture
next ← fn(next)
} yield previousResults :+ next
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment