首页 > 解决方案 > catchError 取消所有并行请求

问题描述

我并行提出了多个请求。然后我在超时时抛出错误。

getAllDirections() {
...
return from(urls).pipe( // a list of api urls
  mergeMap((urlParam, index) =>
    this.http.get<ISchedules>(urlParam[0]).pipe(
      map(dataRes => {
        return dataRes;
      }),
      timeout(6000),
      catchError(error => {
        console.log(error);
        return throwError(`Request timeout ${error}`);
      })
    )
  )
); 

然后在我的效果中,我捕获了错误

$LoadSchedulingsByDirectionsByBranch = createEffect(() =>
  this.actions$.pipe(
    ofType<any>(ESchedulesActions.Type..),
    mergeMap(action =>
      this.apiCallsService.getAllDirections(action.payload, '2010-09-18').pipe(
        map(trips => ({
          type: ESchedulesActions.TypeSuccess,
          payload: trips
        })),
        catchError(err => {
          console.log(err);
          return of({
            type: ESchedulesActions.TypeFail,
            payload: err
          });
        })
      )
    )
  )
);

问题是当它检测到第一次超时时,mergeMap停止并且不处理其他调用。结果,我只得到一个console.log(error) ,而不是在每个 http 请求上都有错误。(只能进去catchError一次)

在我的 http 网络 devtools 上,我注意到当第一次超时发生时,所有并行请求都被取消。

标签: javascriptangularreduxrxjsngrx

解决方案


推荐阅读