如何创建RXjs重试时有延迟和限制尝试

Tom*_*mog 24 observable rxjs angular

我正在尝试进行API调用(使用angular4),它在失败时重试,使用retryWhen.我希望它延迟500毫秒,然后再次重试.这可以通过以下代码实现:

loadSomething(): Observable<SomeInterface> {
  return this.http.get(this.someEndpoint, commonHttpHeaders())
    .retryWhen(errors => errors.delay(500));
}
Run Code Online (Sandbox Code Playgroud)

但这将永远尝试.我怎么限制它,比方说10次?

谢谢!

pau*_*els 53

您需要将限制应用于重试信号,例如,如果您只需要10次重试:

loadSomething(): Observable<SomeInterface> {
  return this.http.get(this.someEndpoint, commonHttpHeaders())
    .retryWhen(errors => 
      // Time shift the retry
      errors.delay(500)
            // Only take 10 items
            .take(10)
            // Throw an exception to signal that the error needs to be propagated
            .concat(Rx.Observable.throw(new Error('Retry limit exceeded!'))
    );
}
Run Code Online (Sandbox Code Playgroud)

编辑

一些评论者询问如何确保最后一个错误是被抛出的错误.答案有点不那么干净,但同样强大,只需使用其中一个展平贴图操作符(concatMap,mergeMap,switchMap)来检查您所在的索引.

注意:使用新的RxJS 6 pipe语法进行将来校对(这在RxJS 5的更高版本中也可用).

loadSomething(): Observable<SomeInterface> {
  const retryPipeline = 
    // Still using retryWhen to handle errors
    retryWhen(errors => errors.pipe(
      // Use concat map to keep the errors in order and make sure they
      // aren't executed in parallel
      concatMap((e, i) => 
        // Executes a conditional Observable depending on the result
        // of the first argument
        iif(
          () => i > 10,
          // If the condition is true we throw the error (the last error)
          throwError(e),
          // Otherwise we pipe this back into our stream and delay the retry
          of(e).pipe(delay(500)) 
        )
      ) 

  return this.http.get(this.someEndpoint, commonHttpHeaders())
    // With the new syntax you can now share this pipeline between uses
    .pipe(retryPipeline)
}
Run Code Online (Sandbox Code Playgroud)

  • 如何将最终错误作为最后一次错误抛出? (2认同)
  • 另外需要注意的是,我们与一位同事进行了一些测试,并注意到 throwError 总是被调用。因此,为了理解这一点,我们来到了另一篇文章:/sf/ask/3786858001/ Called-when-shouldnt TLDR:“iif 的作用不是在其他,但订阅一个 Observable 或另一个” (2认同)

归档时间:

查看次数:

11085 次

最近记录:

6 年,4 月 前