Created
July 20, 2015 15:20
-
-
Save matterche/42191bb696a5a64bc4e2 to your computer and use it in GitHub Desktop.
Non-blocking serial execution of futures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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