- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有一些计算需要一段时间,我可能会把它放在 scala.concurrent.Future
中:
val f = Future { someLongRunningFunction() }
假设我想在计算完成后异步执行其他操作:
f.flatMap{ _ => anotherLongRunningFunction() }
如果 f
的初始 block 失败,我如何在使用 flatMap
或其他组合器时“惯用地”处理这个问题?仅仅是在 flatMap
之前使用 recover
或 onFailure
的情况吗?
我喜欢使用 flatMap
的优雅和简单,但似乎失败场景妨碍了它。
编辑:第二个 future 依赖于第一个,因此是flatMap
。我正在寻找一种解决方案,它可以让我像使用 flatMap
一样优雅地链接,但也可以处理第一个失败。
最佳答案
为 flatMap 引用 scaladoc :
Creates a new future by applying a function to the successful result of this future, and returns the result of the function as the new future. If this future is completed with an exception then the new future will also contain this exception.
注意粗体,这意味着您传递给 flatMap 的任何内容都只会在初始 future 成功完成时执行。
所以你应该只处理整个执行的结果:
val result = future1.flatMap {
result => functionReturningFuture2(result)
}
然后:
result.onFailure // or
result.onSuccess // or
result.recover
关于Scala Futures - flatMap 和 onFailure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30169639/
我正在使用来自 Loopj 的 android-async-http 库,我的代码中没有调用 onFailure,我已经尝试了所有三种方法.. W/JsonHttpRH:onFailure(int,
问题 为什么我可以通过我的 mainActivity 连接到我的服务器,而不是通过 fragment ? 设置 程序 1 - 有效... 现在,我有两个程序。第一个称为 mqtt_test,它包括一个
当从服务器调用 persist() 方法时,在验证电子邮件的唯一性后会引发异常。 问题是调用了客户端的 onSuccess 方法,而不是 onFailure。这是代码。 RequestContext
我正在玩“ListenableFutureCallback”。 onSuccess() 工作正常,但 onFailure 永远不会被调用。下面是一些示例代码。 @Service public clas
有人知道如何从附加到 OkHttp 客户端的拦截器类调用 onFailure 回调吗? 最佳答案 @warnyul 评论是对的。一旦我抛出 IOException,它就会触发失败。 关于Android
我目前正在编写一些单元测试,我想每隔几分钟持续运行一次。如果它们中的任何一个失败了,我想捕获引发的错误并对它们进行一些自定义处理(在我的例子中发送警报)。有这样做的标准方法吗?我一直在查看 unitt
我有一个 AJAX.RouteLink,它具有以下 AJAXOptions new AjaxOptions { HttpMethod = "PO
我一直在开发 Android 应用程序,我正在使用 Retrofit。 如何处理 onFailure(Throwable t) 回调 NoInternetConnection 和 OtherError
我正在试用从 http://loopj.com/android-async-http/ 下载的适用于 Android 的 AndroidAsyncHttp 库并试图找到评估 onFailure(Thr
我在 PageMethod 调用中使用了 onSuccess 和 onFailure。然而,它们都没有被调用,WebMethod 也没有被解雇。 alert("1"); PageMethods.Log
例如,当您在 elasticsearch 中发出异步请求时,onFailure 会返回 Exception。如下所示处理异常是否合适。 @Override
我尝试首先使用 Retrofit 将数据获取到类(class)。但我发现我的程序将进入“onFailure”选项。 错误消息: 2020-03-07 18:54:04.499 17756-17756/
如果我有一些计算需要一段时间,我可能会把它放在 scala.concurrent.Future 中: val f = Future { someLongRunningFunction() } 假设我想
我无法理解这个问题。总是在失败时返回。数据返回状态码200,OnResponse不返回。 回应D/OkHttp: > auth( @Field("username") String u
如果我有一些计算需要一段时间,我可能会把它放在 scala.concurrent.Future 中: val f = Future { someLongRunningFunction() } 假设我想
GWT 的 RPC 调用的 onFailure() 方法中是否可以抛出异常?因为这个方法稍后会在服务器响应后被调用,我很抱歉这里可能会发生一些不好的事情.. 例如: public void metho
我正在创建一个需要用户身份验证的应用程序。我在尝试登录时遇到一个问题。当我输入正确的用户名和密码时,将调用 onSuccess 方法。但是,当我输入错误的字段或空字段时,不会调用 onFailure(
我正在尝试运行一个简单的测试应用程序来学习 Retrofit。 onFailure 方法总是被调用,请帮助我。我所做的一切如下: 主 Activity public class MainActivit
我正在使用 Ajax.BeginForm 调用我的应用程序来运行一些代码。出现错误时,我想返回我将创建的自定义错误消息。 但是到目前为止我还无法做到这一点。这就是我所拥有的: Controller :
我知道这是一个常见问题,但我做了所有这些,仍然没有解决。在我的 MainActivity 中,我收到了来自 ServerService.java 的调用,如下所示: String randomNumb
我是一名优秀的程序员,十分优秀!