我有一个功能,需要期货Future[A]*,我希望它返回一个Future[List[A]].
def singleFuture[T](futures: List[Future[A]]): Future[List[A]] = {
val p = Promise[T]
futures filter { _ onComplete { case x => p complete x /*????*/ } }
p.future
}
Run Code Online (Sandbox Code Playgroud)
而且我还希望类型的未来类型Future[List[A]]在列表期货完成后立即List[Future[A]]完成.
该代码不起作用.我想我应该flatMap在这里使用,因为应该有2个内部循环:一个用于未来,一个用于promise.但是怎么样?
我想不能用的理解,因为我想了解的过程中,在更深一层意义上这里.
我有三个顺序期货,并在这样的理解中使用
val comF = for {
f1 <- future1
f2 <- future2
f3 <- future3
} yield {
// something
}
comF onSuccess { }
comF onFailure {
// ---------------- Here is the problem --------------------------------
//
// How do I know which future failed(throw exception), when the callback comes here ?
// Thanks for the help! Different futures using different exceptions can solve it.
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个像List [Future [T]]这样的未来列表,首先我使用这种方法将它转移到Future [List [T]](为什么这个未来的列表转换为未来的列表转换编译和工作?).然后我得到了未来
val fList: Future[List[T]]
fList on Failure {
// …Run Code Online (Sandbox Code Playgroud)