-
-
Save squito/242f82ad6345e3f85a5b to your computer and use it in GitHub Desktop.
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
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