Await Scala Futures

  • Post author:
  • Post category:Scala

Here’s Scala example code that creates 100 Futures and waits forĀ all of them to complete. This is using theĀ global ExecutionContext.

import scala.concurrent.{Future, Await, future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global

object WaitForAllFutures extends App {

    val futures = (1 to 100).map { i =>
      future {
        Thread.sleep(5000)
        println(s"Future $i done.")
      }
    }

    Await.result(Future.sequence(futures), Duration.Inf)
}