【发布时间】:2021-03-19 14:44:30
【问题描述】:
我正在尝试创建一个具有嵌套依赖可观察对象的方法,其中第二个等待第一个。然后返回一个布尔值。
代码的作用是: 从 store 中获取资产列表,获取根资产,然后从数据库中获取状态,然后更新 UI。
但是发生的情况是,false 的值首先从外部订阅返回,然后 UI 被内部订阅正确更新,就像它应该做的那样。但它应该在完成时返回一个值true,而不是在完成之前返回一个false。
我明白为什么会这样,我需要让外面的那个等待。在查看了与此处类似的许多其他问题后,我尝试使用switchMap 和resultSelector。但是我完全不知道如何在这种情况下正确使用它们,tap。
任何建议将不胜感激。
getAndUpdateHexStatus (startTime, endTime): Observable<boolean> {
this.store.pipe(
select((assets) => assets.dashboard.assets), //get the assets
tap((assets) => {
var rootAsset = assets.filter((value) => this.isRootAsset(value)); //get the root asset
if (rootAsset) {
this.getDetailDataForAssetAndDates(rootAsset[0].assetId, new Date(startTime), new Date(endTime)) //get status data from the DB.
.subscribe((data) => {
this.updateAssetStatus(data); //Update the UI
return of(true);
});
}
}),
takeUntil(this.componentDestroyed$)
)
.subscribe();
return of(false);
}
【问题讨论】:
标签: angular typescript rxjs observable