Node.js http retry do while mechanism I have implemented a simple do/while loop to handle a http request to a third-party. I don’t know when the data I receive will have elements in its array so I retry up to 10 times until the array given to me has data which then I stop and store the data in my database.

My question is in terms of performance is my API going to be affected by having this retry mechanism using a simple do/while loop? If so what is a better way to implement a retry mechanism like this??

public MAX_RETRY = 10;

public async processMetrics(sessionId: any, side: any): Promise &lt;any&gt; {

  try {

    let metrics;
    let retryAttempts = 0;

    do {
      await new Promise((done) =&gt; setTimeout(done, 2000));
      metrics = await this.getMetrics(session._id, sessionRequest._id);
      retryAttempts++;

    } while (!metrics.body.metrics.length &amp;&amp; (retryAttempts &lt; this.MAX_RETRY));

    // Store in DB
  } catch (err) {


  }

}

public async getMetrics(sessionId: any, requestId: any): Promise &lt;any&gt; {

  const url = this.config.backendUrl + "/check/metrics";

  const options = {
    uri: url,
    headers: {
      "X-IDCHECK-SESSION_ID": sessionId,
    },
    body: {},
    json: true,
    resolveWithFullResponse: true,
  };

  const metrics = await request.get(options);

  return metrics;

}


I am calling processMetrics from a different async function.  processMetrics is calling a backend (getMetrics) every 2 seconds, it will retry 10 times to see if the result is ready. If it is I will store something in the database and then return.
In terms of performance this should not be an issue, because async operations don't block the event loop, meaning your server will happily accept incoming requests in between the retries. Once in 2 seconds it will be busy making the request to the 3rd party API, but other than that your will not be blocking the main thread.

(You can verify that by shooting requests to your API while it is in the while loop).