0

In my React Native app, I'm using setInterval to poll an API call every few seconds to achieve a certain goal, like this:

 this.timer = setInterval(() => this.doSomething(), 4000);

The problem is it only starts after 4 seconds. I have read other answers that suggest calling the function immediately and then calling setInterval to execute it again after a delay, but that is not working for me because what if the first call succeeds in achieving what I needed it to do? Then I don't want it to be called again, only once would have been enough.

How do I call it once, then if it fails I start polling it every 4 seconds?

Edit: Forgot to mention specifically that this.doSomething is an async function.

instanceof
  • 1,404
  • 23
  • 30
  • Does this answer your question? [fetch retry request (on failure)](https://stackoverflow.com/questions/46175660/fetch-retry-request-on-failure) – user18309290 Jan 25 '23 at 17:47
  • *what if the first call succeeds in achieving what I needed it to do?* -- `clearInterval()`, just like you presumably call it already somewhere? – tevemadar Jan 25 '23 at 17:51
  • @user18309290 I am using axios library, but it's very much the same, thanks, I'll look into that to see if it can help me – instanceof Jan 25 '23 at 17:54
  • I'd use a library that already implements retries--seems annoying to reimplement something that's a solved problem, especially when good alternatives exist. – Dave Newton Jan 25 '23 at 18:04
  • 1
    @DaveNewton I agree, but I always try to minimise adding new libraries for every small little issue I run into, especially for small issues like this. It seems so simple, there must be an easy native way of doing it, without cluttering my already big app with more packages. – instanceof Jan 25 '23 at 18:08
  • @instanceof "Easy" depends on how well you want to do it. Of course it can be done "natively"; everything in a library is just code. – Dave Newton Jan 25 '23 at 18:12
  • @DaveNewton That is very true, I agree completely, well said. I just enjoy doing it myself as well, I want to understand how it works and practice how to do it myself. – instanceof Jan 25 '23 at 18:22

1 Answers1

1

Execute the function first and then setInterval if it fails. You just need to return from the function whether it succeeded, and add an if statement before using setInterval.

let timer;

function doSomething() {
    if (Math.random() > 0.5) {
        // if success, return true
        console.log("Success")
        if (timer) clearInterval(timer);
        return true;
    } else {
        // if failed, return false
        console.log("Failed")
        return false;
    }
}

let result = doSomething()

if (!result) {
    timer = setInterval(doSomething, 1000);
}

Async version:

let timer;

async function doSomething() {
    if (Math.random() > 0.5) {
        // if success, return true
        console.log("Success")
        if (timer) clearInterval(timer);
        return true;
    } else {
        // if failed, return false
        console.log("Failed")
        return false;
    }
}

doSomething().then(
    (result) => {
        if (!result) {
            timer = setInterval(doSomething, 1000);
        }
    }
)
Fractalism
  • 1,231
  • 3
  • 12