title: How to best leverage setInterval to poll an API endpoint?
body: <p>So i have this endpoint that I'd like to call multiple times until I get my desired response, and I'd like to know if this approach is a valid way to handle it.</p>
  <p>so this endpoint, lets call it <code>/ping</code> returns an object with the value as follows <code>{ping: 1}</code> now I'd like to hit this endpoint a couple more times or until the value of <code>ping</code> changes from <code>1</code> to <code>2</code>. This is my current approach but there's something that does not sit right with me, I'd like to get a second pair of eyes.</p>
  <pre><code>import React from &quot;react&quot;;
  import axios from &quot;axios&quot;;
  
  function App() {
    const apiCall = async () =&gt; {
      try {
        const ping = await axios({
          url: &quot;https://rest.ensembl.org/info/ping?content-type=application/json&quot;,
          method: &quot;GET&quot;,
          headers: {
            accept: &quot;Application/json&quot;,
            &quot;Content-Type&quot;: &quot;Application/json&quot;,
          },
        });
        return ping.data;
      } catch (error) {
        return error.response;
      }
    };
  
    const myIntervalFunc = () =&gt; {
      let myCall = async () =&gt; await apiCall();
      let retryNum = 0;
      myCall();
      let interval = setInterval(async () =&gt; {
        let intervalCall = await myCall();
        if (intervalCall.ping !== 2) {
          retryNum++;
          console.log(&quot;INTERVAL DATA &quot;, intervalCall);
        }
        if (retryNum === 6) {
          clearInterval(interval);
        }
      }, 3000);
    };
  
    return &lt;button onClick={() =&gt; myIntervalFunc()}&gt;Make interval call&lt;/button&gt;;
  }
  
  export default App;
  
  </code></pre>
  <p>As you can see above, when the user presses the button, an API call is made to our <code>/ping</code> endpoint and we retry it 7 times before clearing the interval. Is there a more efficient way or this is good enough? Your feedback is really appreciated.</p>