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

90
Vistas
Axios interceptors runs more than once

I have this interceptors that logs the user out if unauthorized. I do, however, get more than a 401 response, and the interceptors does therefore run for as many times as the responses I get (4). Is there a way to make it run only for the first one?

This is my code:

api.interceptors.response.use(
(response) => response,
(err) => {
  if (err.response.status === 401 && isLoggedIn) {
    api
      .delete("auth/sign_out")
      .then((resp) => {
        clearLocalStorage();
      })
      .catch((err) => {
        clearLocalStorage();
      });
  } else {
    return Promise.reject(err);
  }
  return err;
}

);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You might want something like this to "lock out" the possible re-entrant calls:

let isLoggingOut = false; // global

// ...

api.interceptors.response.use(
  (response) => response,
  async (err) => {
    if (err.response.status === 401 && isLoggedIn) {
      if(!isLoggingOut) {
        isLoggingOut = true;  // disallow re-entrant calls
        try {
          await api.delete('auth/sign_out');
        } catch (deletionError) {
          // throw errors away
        } finally {
          clearLocalStorage();
          isLoggingOut = false;
          isLoggedIn = false; // if the variable is assignable
        }
      }
    }
    return err;
  },
);
about 4 years ago · Juan Pablo Isaza 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