如何展平斯卡拉的期货清单 [英] How to flatten a List of Futures in Scala

查看:72
本文介绍了如何展平斯卡拉的期货清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个价位:

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.

这篇关于如何展平斯卡拉的期货清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆