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.