typescript - switchMap 到 switchMapTo
问题描述
目前我有这段代码
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
变体的行为方式?
解决方案
switchMapTo
switchMap
除了它需要一个可观察的而不是回调函数之外,它是相同的。查看源代码:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/switchMapTo.ts#L56
推荐阅读
- python - 如何使用 np.where 的返回值或在其中应用函数
- spring - 如何在 Spring Webflux 中自定义 Oauth2 的登录页面?
- javascript - 当我在移动设备上触摸我的 JavaScript 滑块时,我无法向下滚动页面
- python - 套接字使用随机端口,我需要一个
- python - .pct_change() 在无限循环中从一个空数据帧开始
- firebase-realtime-database - 我的 gitignor 文件没有停止推送到 GitHub 文件
- javascript - 使用“onclick”函数调用来修改 Javascript 中的变量
- flutter - 如何在没有 ConsumerWidget 或 HookWidget 的情况下在构建外部访问 Riverpod StateNotifier 状态?
- c++ - C++ 中的 #pragma scalar_storage_order 指令被忽略
- r - DataTable 突出显示空单元格