Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda