【问题标题】:How to trigger a catchError and retryWhen/retry - RXJS如何触发 catchError 和 retryWhen/retry - RXJS
【发布时间】:2020-02-26 19:50:15
【问题描述】:

我正在使用 redux-observables 以及 rxjs 和 backoff rxjs 进行重试,现在我有这个史诗,它将在服务器上请求,如果请求失败,它将调度一个 redux 操作并重试该功能。

defer(callApi).pipe(
  catchError((error) => {
    return actions.hasError(error) // dispatch to redux
  }),

  // this should retry the request 10 times
  retryBackoff({
    shouldRetry: true,
    initialInterval: 1000,
    maxRetries: 10
  })

执行上面的代码似乎只做一件事(以先到者为准)。如何做到这一点,以便在出现错误时触发操作并同时重试?

【问题讨论】:

    标签: javascript functional-programming rxjs redux-observable


    【解决方案1】:

    如果retryBackoff 用于从错误中恢复,您可以使用tap 对错误执行副作用而不捕获错误。

    defer(callApi).pipe(
      tap({ error: e => actions.hasError(e) }),
    
      // this should retry the request 10 times
      retryBackoff({
        shouldRetry: true,
        initialInterval: 1000,
        maxRetries: 10
      })
    

    【讨论】:

      【解决方案2】:

      您可以简单地重新抛出 catchError 块,这样它仍然会被 retryBackoff 拾取

      defer(callApi).pipe(
        catchError((error) => {
          return concat(actions.hasError(error), throwError(error))
        }),
      
        // this should retry the request 10 times
        retryBackoff({
          shouldRetry: true,
          initialInterval: 1000,
          maxRetries: 10
        })
      )
      

      【讨论】:

        猜你喜欢
        • 2018-12-26
        • 2020-12-12
        • 1970-01-01
        • 2019-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多