使用新版本的RxJS 6,尤其是管道运算符。当前,使用管道来获取API调用的结果,并将其传递给一系列其他任务。
一切正常,但在遇到问题时似乎无法找到取消或终止管道的方法。例如,我正在使用tap运算符检查该值是否为null。然后,我抛出一个错误,但是管道仍然似乎移至下一个任务,在本例中为concatmap。
因此,如何过早结束或取消管道?提前致谢。
getData(id: String): Observable<any[]> {
return this.http.get<any>(`${this.baseUrl}/path/${id}`).pipe(
tap(evt => {
if (evt == null) {
return throwError(new Error("No data found..."));
}
}),
concatMap(
evt =>
<Observable<any[]>>(
this.http.get<any[]>(
`${this.baseUrl}/path/relatedby/${evt.child_id}`
).map(res =>( {"response1":evt, "response2":res}) )
)
),
retry(3),
catchError(this.handleError("getData", []))
);}
Run Code Online (Sandbox Code Playgroud) 可能是一个基本问题,但我有一个 Angular 应用程序,它进行后端服务调用以检索一些数据,然后使用该数据进行另一个后端服务调用。
第二个服务调用依赖于第一个成功完成,所以我使用 RxJS 中的 concatMap() 函数。
但是,我下面的代码仅返回第二个服务调用的数据。我需要从两个服务调用返回的所有数据。
感觉我搞砸了 .pipe 调用,但没有取得太大进展。提前致谢。
getData(id: String): Observable<any[]> {
return this.http.get<any>(`${this.baseUrl}/path/${id}`).pipe(
concatMap(
evt =>
<Observable<any[]>>(
this.http.get<any[]>(
`${this.baseUrl}/path/relatedby/${evt.child_id}`
)
)
),
retry(3),
catchError(this.handleError("getData", []))
);}
Run Code Online (Sandbox Code Playgroud)