' mergeDelayError'在RxJS 5

时间:2017-06-26 09:55:53

标签: rxjs rxjs5

我在RxJS 4中遇到过这种方法,看起来很有帮助,但是我似乎无法在RxJS 5中找到它。

https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/errors.md

有人知道RxJS 5中是否有类似的行为?

1 个答案:

答案 0 :(得分:0)

您可以像这样添加一个polyfill。

function mergeDelayError(...sources: Array<Observable<any>>) {
    const final = new Subject();
    const catching = sources.map(obs => obs.pipe(
        catchError(e => {
            if (!final.hasError) {
                final.error(e);
            }
            return EMPTY;
        }),
    ));

    return concat(merge(...catching), final);
}

const o1$ = interval(150).pipe(take(5));
const o2$ = throwError(new Error('woops1'));
const o3$ = throwError(new Error('woops2'));

mergeDelayError(o1$, o2$, o3$).subscribe(
    x => console.log('next:', x),
    e => console.log('error:', e),
    () => console.log('completed'),
);

输出是

[Log] next: – 0
[Log] next: – 1
[Log] next: – 2
[Log] next: – 3
[Log] next: – 4
[Log] error: – Error: woops1