【发布时间】:2019-02-19 17:08:31
【问题描述】:
我有一个检索值的函数,在其中我使用 observable。
this.values$ = this.apiService.retrieveIndicatorHistory(this.indicatorName, this.currentPeriod)
.pipe(
tap(_res => console.log(`value: ${_res}`)),
map(
(_res: any) => {
this.indicatorService.changeLoadingIndicator(this.indicatorName, false);
return _res;
}
),
catchError((err: any) => {
this.indicatorService.changeLoadingIndicator(this.indicatorName, false);
return of(err);
})
在同一个文件中,我有一个函数需要 observable 返回的值。
this.valuesCopy = _.cloneDeep(this.values$);
我收到此错误
error TS2322: Type 'Observable<any>' is not assignable to type 'any[]'.
我知道我应该订阅可观察的检索值(在 retrieveIndicatorHistory 函数内)
我想知道 TS 文件中是否有 this.values$ | async 等效项?
【问题讨论】:
-
我们在 TS 文件中没有任何异步等效项。您需要使用 subscribe 才能读取可观察值并对其应用 deepClone。
-
你可以只使用 Promise。
async管道仍然可以工作,您可以在 js 中使用await结果。obs$.pipe(toPromise())
标签: javascript angular typescript asynchronous observable