Rxjs Retry with Delay function

前端 未结 10 2384
忘了有多久
忘了有多久 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:57

    All of this is RxJS 6+


    TL;DR

    You could use the fully tested operator from this package, or scroll down to see the source :)

    npm i rxjs-boost
    
    import { retryWithDelay } from 'rxjs-boost/operators';
    
    obs$.pipe(
      // will retry 4 times with a 1s delay before each try:
      retryWithDelay(1000, 4)
    );
    

    Criteria

    As most (or maybe none) of the other answer didn't meet all of my criteria, I'll list my solution below. Goals:

    • Emits & completes regularly if no error is thrown. ✅
    • Retries x times if an error is thrown. ✅
    • Has a delay of y before each retry. ✅
    • Returns the last emitted error. (A lot of other answers were struggling with this.) ✅
    • Correct typing with strict: true – but this was quite difficult to mess up. ✅

    Solution

    As every other answer we'll use the retryWhen operator to catch the errors. To track the amount of repetitions can use the scan operator. To limit the amount of repetitions we'll simply throw an error inside a map operator.

    The original source uses throwIf, but in that case we could simply use retryWithDelay from rxjs-boost.

    Last we'll use the delay operator to add the delay between the different executions:

    import { MonoTypeOperatorFunction } from 'rxjs';
    import { delay as delayOperator, map, retryWhen, scan } from 'rxjs/operators';
    
    export function retryWithDelay(
      delay: number,
      count = 1
    ): MonoTypeOperatorFunction {
      return (input) =>
        input.pipe(
          retryWhen((errors) =>
            errors.pipe(
              scan((acc, error) => ({ count: acc.count + 1, error }), {
                count: 0,
                error: undefined as any,
              }),
              map((current) => {
                if (current.count > count) {
                  throw current.error;
                }
                return current;
              }),
              delayOperator(delay)
            )
          )
        );
    }
    

    Sources

    • rxjs-boost
    • Original Source
    • Spec File with Tests – probably comes in handy

提交回复
热议问题