结束

时间:2017-02-06 22:30:20

标签: scala

下面的method1是尝试返回两个Future方法someFutureMethod1someFutureMethod2的结果。任何想法如何使这项工作?理想情况下,首先结束的方法应该等到第二个结束,然后返回组合的未来值。

  def method1 ( s: Seq[Int]): Future [(Int,Int)] = {
      s.map { sx =>
          val future = someFutureMethod1 (sx)
          future.map {
            result => result
          }
          val future2 = someFutureMethod2 (sx)
          future2.map {
            result2 => result2
          }
          (result,result2)  // <-- the method should return the two results in the future
       }
  }


def someFutureMethod1 (i: Int) = Future { 
    i + 1
  }

def someFutureMethod2 (i: Int) = Future { 
    i + 2
  }

1 个答案:

答案 0 :(得分:1)

你在地图中结合了Future,所以看起来你必须返回这样的Future的Seq:

def method1 ( s: Seq[Int]): Seq[Future [(Int,Int)]] =
  s.map { sx =>
    val future = someFutureMethod1 (sx)
    val future2 = someFutureMethod2 (sx)
    future.zip(future2)
  }