如何展平斯卡拉的期货清单

如何展平斯卡拉的期货清单

问题描述:

我想使用这个价位:

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

对其执行一些操作(我当时想弄平)

Perform some operation on it (I was thinking flatten)

f.flatten

并获得此结果

scala> f.flatten = List(1,2,3)

如果在这里不适合使用flatten方法,那很好.只要我得到结果.

If the flatten method isn't appropriate here, that's fine. As long as I get to the result.

谢谢!

Future.sequence接受List[Future[T]]并返回Future[List[T]].

你可以做

Future.sequence(f) 

,然后在其上使用maponComplete访问值列表.

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