Rxjs Retry with Delay function

前端 未结 10 2376
忘了有多久
忘了有多久 2020-11-30 06:32

I am trying to use retry with delay function, I expect function will call after 1000ms delay, but it doesnot, what can be error here? look at conso

10条回答
  •  长情又很酷
    2020-11-30 06:51

    I came to this conclusion, in order to retry with other operations in the http pipe

    import {delay as _delay, map, retryWhen} from 'rxjs/operators';
    
    export const delayedRetry = (delay, retries = 1) => retryWhen(result => {
        let _retries = 0;
        return result.pipe(
          _delay(delay),
          map(error => {
            if (_retries++ === retries) {
              throw error;
            }
            return error;
          }),
        );
      },
    );
    

    Usage

        http.pipe(
          delayedRetry(1500, 2),
          catchError((err) => {
            this.toasterService.error($localize`:@@not-saved:Could not save`);
            return of(false);
          }),
          finalize(() => this.sending = false),
        ).subscribe((success: boolean) => {
            if (success === true) {
               this.toasterService.success($localize`:@@saved:Saved`);
            }
          }
        });
    

提交回复
热议问题