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

95
Views
How to avoid inconsistencies in dependent React state when updating based on promises

In the following example, if the "slow" button is clicked immediately followed by a click of the "fast" button, dependsOnA will first be set to "Result from fast promise" since the fast promise resolves first. After about a second the slow promise will resolve and dependsOnA will be set to "Result from slow promise" . What I really want is for the state dependsOnA to always reflect the current state of a. I.e. dependsOnA should always get the value "Result from fast promise" when a is set to true and "Result from slow promise" when a set to false.

What is the best way of accomplishing this?

import * as React from 'react';

const slowPromise: () => Promise<string> = () => {
    return new Promise((resolve) => {
        setTimeout(() => resolve('Result from slow promise'), 2000);
    });
};

const fastPromise: () => Promise<string> = () => {
    return new Promise((resolve) => {
        setTimeout(() => resolve('Result from fast promise'), 1000);
    });
};

export function Example(props: {}) {
    const [a, setA] = React.useState<boolean>();
    const [dependsOnA, setDependsOnA] = React.useState<string>();

    React.useEffect(() => {
        if (a)
            fastPromise().then(setDependsOnA);
        else
            slowPromise().then(setDependsOnA);
    }, [a]);

    return (
        <div>
            <button onClick={() => setA(false)}>Slow</button>
            <button onClick={() => setA(true)}>Fast</button>
            {dependsOnA}
        </div>
    );
}

I came up with the following solution which uses the cleanup callback of the useEffect hook to "cancel" the state update after the promise has resolved if a has changed since the promise was created. However, this solution feels a bit hacky.

    React.useEffect(() => {
        let isCanceled: boolean = false;
        if (a)
            fastPromise().then((res) => {
                if (!isCanceled) setDependsOnA(res);
            });
        else
            slowPromise().then((res) => {
                if (!isCanceled) setDependsOnA(res);
            });
        return () => { isCanceled = true; };
    }, [a]);
about 4 years ago · Juan Pablo Isaza
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!