How to flatten a List of Futures in Scala

前端 未结 1 1312
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 19:26

I want to take this val:

val f = List(Future(1), Future(2), Future(3))

Perform some operation on it (I was thinking flatten)



        
相关标签:
1条回答
  • 2020-12-08 19:59

    Future.sequence takes a List[Future[T]] and returns a Future[List[T]].

    You can do

    Future.sequence(f) 
    

    and then use map or onComplete on it to access the list of values.

    0 讨论(0)
提交回复
热议问题