Blog>
Snippets

Combining Retry and Debounce Strategies in Redux-Saga

Showcase a complex scenario where both retry and debounce strategies are used together in Redux-Saga, such as for a live search feature with error handling.
import { call, put, debounce, takeLatest } from 'redux-saga/effects';
import { fetchResultsSuccess, fetchResultsFailure } from './actionCreators';
import { searchApi } from './api';
Import necessary effects from redux-saga and action creators for handling success and failure. The 'searchApi' function represents the API call to fetch search results.
function* handleSearch(api, action) {
  try {
    const results = yield call(api, action.payload);
    yield put(fetchResultsSuccess(results));
  } catch (error) {
    yield put(fetchResultsFailure(error.message));
  }
}
Define a generator function 'handleSearch' that makes the API call and handles the response by dispatching either success or failure actions.
function* retrySearchSaga(action) {
  for (let i = 0; i < 3; i++) {
    try {
      yield call(handleSearch, searchApi, action);
      break; // Exit loop on success
    } catch (error) {
      if (i === 2) yield put(fetchResultsFailure(error.message));
      // On the last retry attempt, dispatch a failure action
    }
  }
}
Wraps the 'handleSearch' call in a retry loop, attempting the action up to 3 times before finally failing.
function* debounceSearchSaga() {
  yield debounce(500, 'SEARCH_REQUESTED', retrySearchSaga);
}
Implements the debounce strategy by waiting 500ms after the last 'SEARCH_REQUESTED' action before calling the 'retrySearchSaga'. This prevents the app from making too many calls to the server while the user is typing.
export function* rootSaga() {
  yield takeLatest('SEARCH_REQUESTED', debounceSearchSaga);
}
Root saga that watches for 'SEARCH_REQUESTED' actions and invokes the debounce saga. This setup ensures that only the latest search request is processed, ignoring any requests made in quick succession.