我有几个 observable 与 combineLatest组合在一个 observable 中。此外,我还有一个内部switchMap
可观察对象,在实际示例中向远程服务器发出 http 请求。
现在我想知道,如果switchMap
返回错误,组合的可观察对象如何继续工作?
//emit every 2.5 seconds
const first = interval(2500);
//emit every 2 seconds
const second = interval(2000);
//emit every 1.5 seconds
const third = interval(1500);
//emit every 1 second
const fourth = interval(1000);
let count = 0;
//emit outputs from one observable
const example = combineLatest(
first.pipe(mapTo("FIRST!")),
second.pipe(mapTo("SECOND!")),
third.pipe(mapTo("THIRD")),
fourth.pipe(mapTo("FOURTH"))
)
.pipe(
switchMap(data => {
console.log(data);
count++;
// here lets asume in some cases http request getting error
return count < 5 ? of("switchMap") : throwError("This is an error!");
}),
catchError(err => of(err))
)
.subscribe(val => console.log(val));
输出
["FIRST!", "SECOND!", "THIRD", "FOURTH"]
switchMap
["FIRST!", "SECOND!", "THIRD", "FOURTH"]
switchMap
["FIRST!", "SECOND!", "THIRD", "FOURTH"]
switchMap
["FIRST!", "SECOND!", "THIRD", "FOURTH"]
switchMap
["FIRST!", "SECOND!", "THIRD", "FOURTH"]
This is an error!
所以在得到error combineLatest
observable的工作停止之后。在我的真实示例中,我有 4 个过滤器,在更改过滤器后,我发出 http 请求。