- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个网页,其中有一堆用户可以点击的项目。单击任何项目,取决于它的类型,将向服务器发送 ajax 请求,然后显示更多项目。如果请求导致错误,我想显示它,然后允许用户像以前一样继续单击或与页面交互。
我的代码是这样的
$scope.$createObservableFunction("clickHandler")
.flatMapLatest(function (args) {
//send the ajax request to the server
})
.retry()
.subscribe(function (data) {
//handle getting the data from the server
})
我究竟在哪里可以处理错误情况?我预计会发生错误,并且我一直想重新订阅源,但我希望有机会处理该错误。
最佳答案
诀窍是将错误转化为数据:
$scope.$createObservableFunction("clickHandler")
.flatMapLatest(function (args) {
//send the ajax request to the server
var ajaxQuery = someObservable;
// turn the observable into
// a stream of eithers, which will
// either have a 'result'
// or an 'error'
// use .catch() to turn the error
// into a either with an error property
// use .map() to turn the success
// into a either with the result property
return ajaxQuery
.map(function (result) {
return { result: result };
})
.catch(function (error) {
return Rx.Observable.of({ error: error });
});
})
.subscribe(function (data) {
if (data.error) {
// display data.error to user
}
else {
// display data.result to user
}
})
如果您的 ajax 方法返回一个 Promise
,请使用 then
链接:
$scope.$createObservableFunction("clickHandler")
.flatMapLatest(function (args) {
//send the ajax request to the server
var ajaxQuery = somePromise;
// turn the promise into
// a promise of eithers, which will
// either have a 'result'
// or an 'error'
return ajaxQuery
.then(function onSuccess(result) {
return { result: result };
}, function onError (error) {
return { error: error };
});
})
.subscribe(function (data) {
if (data.error) {
// display data.error to user
}
else {
// display data.result to user
}
})
关于javascript - 使用 flatMapLatest 处理错误并重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876829/
场景: 用户使用组合成单个流的过滤器 当过滤器发生变化时,触发后端事件以获取“廉价”数据 当“廉价”数据到达时,另一个具有相同参数的请求被触发到不同的端点,返回“昂贵”数据,这些数据将用于丰富廉价数据
如何在 C# Reactive 扩展中模拟 flatMapLatest。例如,我需要将此代码重写为 C#: screenChanged .flatMapLatest { retu
我有以下 RxJS 定义: onIds .flatMapLatest((ids) => Rx.Observable .from(ids) .flatMap((id) =
我正在使用 RxSwift Playground: https://github.com/ReactiveX/RxSwift 我实现了以下示例代码来建模一个简单的异步任务,将 Int 的 Observ
我正在尝试编写代码,对特定用户操作执行某种可失败的长时间运行的请求,但在请求处于事件状态时忽略悲伤的操作。目标是只要请求没有成功发出,就允许点击按钮。所有错误都会被跳过。到目前为止,我编写了这样的代码
View 模型: searchButtonInDidTapSubject.withLatestFrom(retailIdSubject.map { text in return tex
我有一个网页,其中有一堆用户可以点击的项目。单击任何项目,取决于它的类型,将向服务器发送 ajax 请求,然后显示更多项目。如果请求导致错误,我想显示它,然后允许用户像以前一样继续单击或与页面交互
在与 flatMapLatest 中的 observables 结合时遇到问题 逻辑:在每个事件的下一个事件上,我想将它与下一个 getCurrentLocation 事件结合在一起,该事件发生在 a
使用 RxJs 我正在尝试重现一个演示。但我没有得到任何结果。而是出现错误:distinct.flatMapLatest is not a function 我在这里做错了什么?我需要在此处添加任何库
我有一些测试函数,观察者对其进行赋值并将其映射到另一个 Observable : private func test1() { selectedTagsVariable .as
每当我的用户位置发生变化时,我都会尝试从网络中获取一些数据。 struct CityService { private init() {} static let shared = CitySe
我无法区分这三个运算符之间的确切区别。 文档: FlatMapMerge FlatMapConcat FlatMapLatest 这些文档链接表明两个 Flow 被平面映射到一个 Flow 对象中。我
我正在关注 RxSwift 示例 GitHub SignUp using Driver , 并以类似的方式结束。 我有一个执行身份验证并返回 Observable 的网络请求,这样我就有了: publ
我学习了 RxSwift 中的示例代码。在文件 GithubSignupViewModel1.swift 中,validatedUsername 的定义是: validatedUsername = i
在我的 Swift UIViewController 中,我尝试订阅 Variable 类型的类成员,通过 flatMapLatest 调用运行它,然后在所有订阅者上执行 flatMapLatest
我有 2 个嵌套的 Observable Streams,它们执行 HTTP 请求。现在我想显示加载指示器,但无法使其正常工作。 var pageStream = Rx.createObservabl
我主要是 C# 开发人员,正在扩展我在 JavaScript 方面的视野,最近偶然发现了一个名为 RxJS 的库。 我想了解 Map、Flatmap、FlatmapLatest 之间的关系以及 C#.
我是一名优秀的程序员,十分优秀!