Kwinten
Kwinten

Reputation: 305

Redux observable retry does not resend the API call

I am using redux-observable and want to retry 3 times whenever an API call throws error.

But it doesn't retry, it has only one http request sent.

I made up a sample calling github user api to find a user, if you provide a non exist username like This doesn't exist then it will throw an 404 error. I have added retry(3) but it doesn't retry.

You can find the codes on codesandbox

export const fetchUserEpic = action$ => action$.pipe(
  ofType(FETCH_USER),
  mergeMap(action =>
    ajax.getJSON(`https://api.github.com/users/${action.payload}`).pipe(
      map(response => fetchUserFulfilled(response))
    )
  ),
  retry(3)
);

Upvotes: 1

Views: 536

Answers (1)

evilstiefel
evilstiefel

Reputation: 496

Move your retry up into the inner observable, like so:

export const fetchUserEpic = action$ => action$.pipe(
  ofType(FETCH_USER),
  mergeMap(action =>
    ajax.getJSON(`https://api.github.com/users/${action.payload}`).pipe(
      map(response => fetchUserFulfilled(response)),
      retry(3)
    )
  )
);

Your action$ doesn't actually fail, it's the ajax-call you want to retry.

Upvotes: 4

Related Questions