How to resolve a list of futures in Scala

后端 未结 3 1517
遇见更好的自我
遇见更好的自我 2021-02-02 11:05

I have a call that returns a Future. However, I need to make n calls so I will get back n futures. I am wondering how I would get the futures to all resolve before proceeding (w

3条回答
  •  滥情空心
    2021-02-02 11:53

    A little more functional:

    val futures = for {
      c <- 0 until 10
       } yield {
        val f = call(c) 
        f onSuccess {
          case x =>
          // Do your future stuff with x
        }
        f
      }
    Future.sequence(futures)
    

提交回复
热议问题