Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
How to migrate api call wrapper from a redux thunk to redux saga

I have recently started using redux-saga and I'm really liking it.

I have the following wrapper which I was using for my api calls which would take a promise (my api call), and display a preloader and handle errors.

export const callApi = (promise: Promise<any>, errorMsg: string = 'Api error') => (dispatch: Dispatch) => {
  dispatch(setLoading(true));
  return promise.then(
    (response) => {
      dispatch(setLoading(false));
      return response.body;
    },
    (error) => {
      dispatch(setLoading(false));
      dispatch(apiError(errorMsg, error));
      return error;
    });
};

I'm unsure how I would replicate behaviour like this in redux saga. I couldnt find any example of doing anything like this?


So far I've come up with

const camelizeKeysPromise = (obj) => Promise.resolve(camelizeKeys(obj));


export function* sagaCallApi(promise: Promise<any>, errorMsg: string = 'Api error') {
   yield put(setLoading(true));
   try {
      const response = yield call(promise);
      try {
        const result = yield call(camelizeKeysPromise(response.body));
        return result;
      } catch (e) {
        return response.body;
      }
   } catch (exception) {
      yield put(setLoading(false));
      yield put(apiError(errorMsg, error));
    };
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Yielding a call to promise will not return the desired response. You can use eventChannel from redux-saga to create a channel that emits the response on success or the error object on failure and then subscribe to the channel in your saga.

const promiseEmitter = promise => {
  return eventChannel(emit => {
    promise.then(
      response => emit({response}),
      error => emit({error})
    );
  });
};

Modify your new saga by replacing the call to the promise with this:

const channel = yield call(promiseEmitter, promise);
const {response, error} = yield take(channel);
if(response){
  // handle success
  return response;
}else if(error){
  // handle failure
  yield put(setLoading(false));
  yield put(apiError(errorMsg, error));
}

Be aware that there might be syntactical errors in my code as I wrote this without an editor, but you can get the general approach.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda