Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!