目前我有这段代码
this.save()
.pipe(switchMap(() => this.unlock()))
.subscribe(...);
save
和unlock
实现是
private save(): Observable<void> {
return new Observable(subscriber => {
this.xmlService.save(..., () => {
...
subscriber.next();
subscriber.complete();
});
});
}
private unlock(): Observable<void> {
return this.httpService.unlock(this.id);
}
unlock
并不真正依赖于save
返回值,我可以使用switchMapTo
吗?
this.save()
.pipe(switchMapTo(this.unlock()))
.subscribe(...);
还是我误解了*To
变体的行为方式?
答案 0 :(得分:1)
switchMapTo
与switchMap
相同,除了它采用可观察的函数而不是回调函数。
查看源代码:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/switchMapTo.ts#L56