小编pyt*_*ner的帖子

使用 scala future 进行异常处理

我对 Scala Future 有疑问。我有一个带有两个参数的函数:x: Future[Int] 和 y: Future[Int]。

该函数应该返回一个以以下内容完成的 future:

  • 如果 x 成功完成,则 x 的值,
  • 如果 x 失败且 y 成功完成,则 y 的值,
  • 如果 x 失败并且 y 失败,则 y 失败的异常。

我似乎不知道该怎么做。

目前的代码:

def myFunction(x: Future[Int], y: Future[Int]): Future[Int] = {
  x.onSuccess {
    case result => return Future(result)
  }
  x.onFailure {
    case e => 
      y.onSuccess {
        case res => return Future(res)
      }
      y.onFailure {
        case f => throw f
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

scala future

0
推荐指数
1
解决办法
2197
查看次数

标签 统计

future ×1

scala ×1