Angular - 管道函数中断执行流程
Angular - pipe function break execution flow
我有以下代码:
subscriber.pipe(
switchMap(data => {
this.getData().pipe(
map(() => res),
catchError(error => {
return of(null);
})
);
}),
switchMap(data => {
})
).subscribe();
如果第一个 switchMap 出错,我将返回 of(null),因此下一个 switchMap 正在将数据接收为 null。
但是我喜欢停止执行,以防第一个 switchMap 进入 catchError 块,它不应该执行第二个 switchMap。有办法实现吗?
将 catchError
放在管道的末端。
subscriber.pipe(
switchMap(data => {
...
}),
switchMap(data => {
...
}),
catchError(error => {
return of(null);
})
).subscribe(val => {
// val will be null if any error happened
})
我想你可以在这里使用过滤器。从捕获错误中你可以 return null 并且在第一个 switchMap 之后你可以使用过滤器来过滤掉 null.
subscriber.pipe(
switchMap(data => {
this.getData().pipe(
map(() => res),
catchError(error => {
return null;
})
);
}),
filter(v => v),
switchMap(data => {
})
).subscribe();
RxJS # 空
EMPTY 是一个不发射任何东西并立即完成的可观察对象。
subscriber.pipe(
switchMap(data => {
this.getData().pipe(
map(() => res),
catchError(_ => EMPTY)
);
}),
switchMap(data => {
})
).subscribe();
我有以下代码:
subscriber.pipe(
switchMap(data => {
this.getData().pipe(
map(() => res),
catchError(error => {
return of(null);
})
);
}),
switchMap(data => {
})
).subscribe();
如果第一个 switchMap 出错,我将返回 of(null),因此下一个 switchMap 正在将数据接收为 null。 但是我喜欢停止执行,以防第一个 switchMap 进入 catchError 块,它不应该执行第二个 switchMap。有办法实现吗?
将 catchError
放在管道的末端。
subscriber.pipe(
switchMap(data => {
...
}),
switchMap(data => {
...
}),
catchError(error => {
return of(null);
})
).subscribe(val => {
// val will be null if any error happened
})
我想你可以在这里使用过滤器。从捕获错误中你可以 return null 并且在第一个 switchMap 之后你可以使用过滤器来过滤掉 null.
subscriber.pipe(
switchMap(data => {
this.getData().pipe(
map(() => res),
catchError(error => {
return null;
})
);
}),
filter(v => v),
switchMap(data => {
})
).subscribe();
RxJS # 空
EMPTY 是一个不发射任何东西并立即完成的可观察对象。
subscriber.pipe(
switchMap(data => {
this.getData().pipe(
map(() => res),
catchError(_ => EMPTY)
);
}),
switchMap(data => {
})
).subscribe();