小编Chr*_*sse的帖子

在Scala中按顺序组合任意数量的期货

我是scala的新手,我尝试在scala 2.10RC3中组合几个Futures.本Futures应该按顺序执行.在文档Scala SIP14中,andThen定义方法以便按顺序执行Futures.我用这种方法组合了几种Futures(见下面的例子).我的期望是它打印6但实际上结果是0.我在这做错了什么?我有两个问题:

首先,结果为什么0.其次,我如何组合几个Futures,以便第二个的执行Future在第一个Future完成之前不会开始.

val intList = List(1, 2, 3)

val sumOfIntFuture = intList.foldLeft(Future { 0 }) {
 case (future, i) => future andThen {
  case Success(result) => result + i 
  case Failure(e) => println(e)
 }
}

sumOfIntFuture onSuccess { case x => println(x) }
Run Code Online (Sandbox Code Playgroud)

concurrency scala future

10
推荐指数
1
解决办法
4032
查看次数

标签 统计

concurrency ×1

future ×1

scala ×1