【发布时间】:2014-05-22 01:48:35
【问题描述】:
根据标题,给定一个IndexedSeq[Future[Foo]],我想生成一个在原始IndexedSeq 的所有未来都完成时完成的未来。最好的方法是什么?如果列表中有 Future.sequence 之类的东西,有没有一种很好的方法来链接调用?
考虑两种情况:
bar.createFuture(blah) // Future[A]
.flatMap(f1) // f1: A -> Future[B]
.flatMap(f2) // f2: B -> Future[C]
.map(f3) // f3: C -> IndexedSeq[Future[D]],
// but would like to call flatMap(f3...???)
bar.createIndexedSeq(blah) // IndexedSeq[A]
.map(f1) // f1: A -> B
.map(f2) // f2: B -> C
.map(f3) // f3: C -> Future[D]
.??? // convert IndexedSeq[Future[D]]
// into Future[IndexedSeq[D]]
.map(f4) // f4: IndexedSeq[D] -> E
【问题讨论】:
-
这不会为 Reactive Beauty 赢得价格,但我经常做的是为每个未来创建一个计数的 CountDownLatch。当每个未来完成(好或坏)时,它会减少计数。
标签: scala