我看到CompletableFuture
有一個方法handle
是相同階Future
的handle
基本上轉換成功和例外所有成功是map
和flatMap
上游的(或在java世界中的thenApply
和thenCompose
)。在Java中,什麼是twitter未來rescue
(或者scala未來recoverWith
)的等價物?什麼是Java CompletableFuture等效階未來援救的和處理
rescue
在斯卡拉基本上就像老的java try....catch
,然後重新拋出更多的信息,所以它可以很好用。例如,在twitterFuture.handle
或scalaFuture.recover
返回單位是U
,因此您返回響應。在twitterFuture.rescue
或scalaFuture.recoverWith
,它返回Future[U]
所以可以採取一定的例外情況,添加更多的信息,並返回Future.exception(xxxxx)
我沒有在Scala的API文檔中看到任何叫做'rescue'的東西,你的意思是['recover'](http://www.scala-lang.org/api/current/index.html#scala.concurrent.Future @recover [U%3E:T](pf:PartialFunction [Throwable,U])(implicitexecutor:scala.concurrent.ExecutionContext):scala.concurrent.Future [U])(儘管這更像'try/catch' '嘗試/ finally')? – kennytm
@kennytm http://twitter.github.io/util/docs/#com.twitter.util.Future –