Open Christian-health opened 7 years ago
recover from an onError notification by continuing the sequence without error 通过继续序列而无错误地从onError通知中恢复
The Catch operator intercepts an onError notification from the source Observable and, instead of passing it through to any observers, replaces it with some other item or sequence of items, potentially allowing the resulting Observable to terminate normally or not to terminate at all. Catch操作符拦截源Observable的onError通知,而不是将其传递给任何观察者,而是将其替换为某些其他项目或项目的序列,可能允许导致Observable正常终止或根本不终止。
There are several variants of the Catch operator, and a variety of names used by different ReactiveX implementations to describe this operation, as you can see in the sections below. Catch运算符有几种变体,以及不同的ReactiveX实现用于描述此操作的各种名称,您可以在下面的部分中看到。
In some ReactiveX implementations, there is an operator called something like “OnErrorResumeNext” that behaves like a Catch variant: specifically reacting to an onError notification from the source Observable. In others, there is an operator with that name that behaves more like a Concat variant: performing the concatenation operation regardless of whether the source Observable terminates normally or with an error. This is unfortunate and confusing, but something we have to live with. 在一些ReactiveX实现中,有一个叫做OnErrorResumeNext的操作符? 它的行为类似于Catch变体:特别对源Observable的onError通知做出反应。 在其他方面,具有该名称的运算符的行为更像Concat变体:执行连接操作,而不管源Observable是否正常终止或出现错误。 这是不幸的和混乱的,但我们必须要承受的东西。
RxJS implements the Catch operator with two distinct operators: RxJS使用两个不同的运算符来实现Catch运算符: catch instructs an Observable to begin emitting a second Observable sequence if it encounters an error onErrorResumeNext 指示Observable在遇到错误时开始发出第二个可观测序列 instructs an Observable to begin emitting a second Observable sequence if it encounters an error or if the source Observable terminates normally 指示Observable开始发出第二个可观测序列,如果遇到错误或源Observable正常终止
This implementation borrows the confusing nomenclature from Rx.NET, in which onErrorResumeNext switches to a back-up Observable both on an error and on a normal, error-free termination of the source Observable. 这个实现借用了Rx.NET的令人困惑的命名法,其中onErrorResumeNext在源Observable上的错误或者是正常的,无错误的终止上切换到备份Observable。
如果在这个页面:http://reactivex.io/documentation/observable.html找不到一个操作符的demo代码举例。 那么就去github上找 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/onerrorresumenext.md
Rx.Observable.onErrorResumeNext(...args)
Continues an observable sequence that is terminated normally or by an exception with the next observable sequence or Promise. 这个函数的意思是:var source = Rx.Observable.onErrorResumeNext(source1, source2, source3); 这个函数的参数一个Observable队列,例如是上面的source1,source2,source3等等。然后一个一个执行这个队列中的每一个Observable,比如先执行source1,然后执行source2,然后执行source3。 如果一个正常的终止,或者异常的终止都继续执行其他的。比如如果source1执行正常的终止,那么接着执行source2和source3,如果执行source1没有能够正常的终止,那么也能够继续执行正常的执行source2和source3等等。
args
(Array|arguments): Observable sequences to concatenate.
用来链接的可观察序列(Observable
): An observable sequence that concatenates the source sequences, even if a sequence terminates exceptionally.
var source1 = Rx.Observable.throw(new Error('error 1'));
var source2 = Rx.Observable.throw(new Error('error 2'));
var source3 = Rx.Observable.return(42);
var source = Rx.Observable.onErrorResumeNext(source1, source2, source3);
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: 42
// => Completed
File:
Dist:
Prerequisites:
NPM Packages:
NuGet Packages:
Unit Tests:
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> -->
<script type="text/javascript" src="./rxjs/dist/rx.all.js"></script>
</head>
<body>
<script>
var source1 = Rx.Observable.throw(new Error('error 1'));
var source2 = Rx.Observable.throw(new Error('error 2'));
var source3 = Rx.Observable.return(42);
var source4 = Rx.Observable.throw(new Error('error 4'));
var source5 = Rx.Observable.return(45);
var source = Rx.Observable.onErrorResumeNext(source1, source2, source3,source4,source5);
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
</script>
</body>
</html>
最终的结果是:
Navigated to file:///media/B/rxjs-learn/demo.html .html:22
Next: 42 demo.html:22
Next: 45 demo.html:28
Completed
cache (Replay)
ensure that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items 确保所有观察者看到相同的发射物品序列,即使它们在Observable已经开始排放物品之后订阅
A connectable Observable resembles an ordinary Observable, except that it does not begin emitting items when it is subscribed to, but only when the Connect operator is applied to it. In this way you can prompt an Observable to begin emitting items at a time of your choosing. 一个Connectable(可连接的)Observable类似于一个普通的Observable,除非它在订阅时不开始发布项目,而且只有当Connect运算符被应用到它时。 这样,您可以提示Observable在您选择的时间开始发送物品。
If you apply the Replay operator to an Observable before you convert it into a connectable Observable, the resulting connectable Observable will always emit the same complete sequence to any future observers, even those observers that subscribe after the connectable Observable has begun to emit items to other subscribed observers. 如果你应用replay操作符到一个Observabe,在你转换它为一个可链接的Observable之前, 所得到的可连接Observable将始终向任何未来的观察者发出相同的完整序列,即使是在可连接的Observable已经开始向其他订阅的观察者发射物品之后订阅的观察者。
In RxJs the replay operator takes four optional parameters and returns an ordinary Observable: 在RxJs中,replay运算符使用四个可选参数并返回一个普通的可观察值:
a transforming function that takes an item emitted by the source Observable as its parameter and returns an item to be emitted by the resulting Observable 一个转换函数,这个转换函数使用源Observable 发出的一个item作为参数,并且返回一个item
the maximum number of items to buffer and replay to subsequent observers
the age, in milliseconds, at which items in this buffer may be discarded without being emitted to subsequent observers
the Scheduler on which this operator will operate