1

I have the below code where checkReady() is called every 5 seconds until the response comes back as true.

But when I test this function, it does not appear to be waiting 5 seconds between each call to checkReady(). How can I correctly implement this to wait between each call ?

async isReady(ID) {

        try {
            const checkReady = async (value) => {

                console.log('checkReady = ' + value);

                if (value > 0) {

                    let response = await client.get(`/user/account/isReady/${ID}`);

                    let ready = response.data;

                    if (ready) {
                        this.Ready = true;
                        return;
                    }

                    setTimeout(await checkReady(value - 1), 5000);
                }
            }

            await checkReady(10);
        }
        catch (e) {
            console.error(e);
        }
    }
Jyina
  • 2,530
  • 9
  • 42
  • 80
  • 2
    `setTimeout` takes a function – Daniel A. White Mar 14 '23 at 20:25
  • 3
    See this: [Why is the method executed immediately when I use setTimeout?](https://stackoverflow.com/questions/7137401/why-is-the-method-executed-immediately-when-i-use-settimeout) You should promisify `setTimeout` (`const timeout = t => new Promise(r => setTimeout(r, t))`) and `await` that. – FZs Mar 14 '23 at 20:30

0 Answers0