1
我有以下代碼:動態創建期貨的理解和等待完成
// Start async functions
val async1: Future[Seq[Int]] = ...
val async2: Future[Seq[Int]] = ...
val async3: Future[Seq[Int]] = ...
// Wait for completion
(for {
a1 <- async1
a2 <- async2
a3 <- async3
} yield (a1, a2, a3)).map {
// Use the results
}
我想提高這個處理的異步函數的變量量(不一定要求每一次他們每個人) 。什麼是迄今爲止我所做的是:
// Start the async functions ?
val asyncs: Seq[Future[Seq[Int]] = otherList.filter(x => someCondition).map(x => asyncFunc(x))
// Wait for the functions to finish ?
(for (seqInt <- asyncs) yield seqInt).map {
case results => // <-- problem here
// Use the results
}
我遇到的問題是,結果是Future[Seq[Int]]
類型,但我預計他們會(Seq[Int], Seq[Int], Seq[Int])
類型的像在第一個片段。
在我想這樣做到底是開球的異步功能,這都具有相同的Future
返回類型的動態量,等待他們全部結束,然後利用其所有的結果一起。
'Future.sequence(異步操作)'關鍵部分 – cchantep