Skip to content

Instantly share code, notes, and snippets.

@squito
Forked from ryanlecompte/gist:6313683
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squito/242f82ad6345e3f85a5b to your computer and use it in GitHub Desktop.
Save squito/242f82ad6345e3f85a5b to your computer and use it in GitHub Desktop.
import scala.concurrent.{Future, Promise}
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.concurrent.{Await, Promise}
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global
def performSequentially[A](items: Seq[A])(f: A => Future[Unit]): Future[Unit] = {
items.headOption match {
case Some(nextItem) =>
val fut = f(nextItem)
fut.flatMap { _ =>
performSequentially(items.tail)(f)
}
case None =>
// nothing left to process
Future.successful(())
}
}
val items = 0 to 5
val seqFuture = performSequentially(items) { i => Future { Thread.sleep(1000); println("hello " + i) } }
Await.result(seqFuture, Duration.Inf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment