RxJS 重试然后 catchError 不工作

RxJS retry and then catchError not working

我有一个可观察的:

public updateThingName(name: string, thingId: number): Observable<any> {
    console.log('attempting rename');
    return this.http
        .put(
          `${this.thingApi}/projects/${thingId}`, 
          { name },
          this.options,
      ).pipe(
          map(response => response.data.id)
      );
}

作为较长链的一部分调用:

    return this.projectService.getProject(id).pipe(
            switchMap(prevProjectData => {
                return this.projectService.updateProject(id, data).pipe(
                    map(newProjectData => ({prevProjectData, newProjectData}))
                )
            }),
            switchMap(({prevProjectData, newProjectData}) => {
                return this.thingService.updateThingName(newProjectData.title, newProjectData.thingId)
                  .pipe(retry(5),catchError(err => {
                      return this.projectService.revertProjectUpdate(err, prevProjectData);
                }))
            }),
            tap(() => { ... save to logs only when successful ... })
        );

我想尝试重命名一些东西,如果失败重试 5 次,如果仍然失败则捕获错误,还原之前的更改并在还原函数中抛出最终错误。将错误响应还原并发送回前端工作正常,但无论我将 retry(5) 放在哪里,我都只会在日志中看到初始的 console.log('attempting rename');

我错过了使用重试吗?我如何让它工作?

这是 NestJS 上的后端代码,所以我不会直接处理最终的订阅方面,如果有区别的话。

谢谢!

应该是正确的。该方法调用一次,但它尝试重新订阅多次。如果您在每次订阅时都记录一条消息,您应该能够看到它:

public updateThingName(name: string, thingId: number): Observable<any> {
  return defer(() => {
    console.log('attempting rename');
    return this.http
      .put(
        `${this.thingApi}/projects/${thingId}`, 
        { name },
        this.options,
    ).pipe(
        map(response => response.data.id)
    );
  )};
}