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

233
Vistas
Stop executing function after data change (React, useEffect)

I have a function (startLoad()) which is executed every time some of my data changes (i.e. dataMayChange). Within this method i want to execute some GraphQL Queries (i.e. fireQuery1, fireQuery2, ...). Every time when my dataMayChange changes, i want to stop executing startLoad and execute it again from the beginning. This way i want to prevent the old queries before my dataMayChange has changed to be fired.

Here is my Code:

 React.useEffect(() => {   
        startLoad()     
    // eslint-disable-next-line
}, [dataMayChange])

const startLoad = async() => {
  await fireQuery1
  await fireQuery2
  await fireQuery3
  await fireQuery4
}

Does anyone have an idea how i can achieve this behavior?

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

0

What you're looking for is a flag or signal you can provide to startLoad to tell it to stop, and a cleanup callback on useEffect.

You can do a bespoke flag or signal, or use AbortController. Here's an example of the latter but roll-your-own would look quite similar:

React.useEffect(() => {   
    const controller = new AbortController();
    startLoad(controller.signal);
    return () => {
        // This function is called when the data is changing and the
        // effect callback is about to be re-run
        controller.abort();
    };
    // eslint-disable-next-line
}, [dataMayChange]);

const startLoad = async (signal) => {
    await fireQuery1;
    if (signal.aborted) {
        return;
    }
    await fireQuery2;
    if (signal.aborted) {
        return;
    }
    await fireQuery3;
    if (signal.aborted) {
        return;
    }
    await fireQuery4;
};

That's quite repetitive, you probably want to put your actions in an array and loop through it. It's hard to show that given that fireQuery1 etc. are obviously just conceptual placeholders, but for instance:

const startLoad = async (signal) => {
    const tasks = [doThis, doThat, doTheOther, doSomethingElse];
    for (const task of tasks) {
        if (signal.aborted) {
            return;
        }
        await task(); // You can even keep track of its result to pass to the next if relevant, etc.
    }
};

If any of those actions involve things (like fetch) that support AbortController/AbortSignal, you can also pass the signal on to those things.

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