簡體   English   中英

如何觸發 catchError 和 retryWhen/retry - RXJS

[英]How to trigger a catchError and retryWhen/retry - RXJS

I am using redux-observables together with rxjs and backoff rxjs for retry, now I have this epic where it will request on server, if the request fails it will dispatch a redux action and will retry the function.

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
  })

執行上面的代碼似乎只做一件事(以先到者為准)。 我怎樣才能做到這一點,以便在出現錯誤時觸發操作並同時重試?

如果使用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
  })

你可以簡單地從你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
  })
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM