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 <any> {

  try {

    let metrics;
    let retryAttempts = 0;

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

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

    // Store in DB
  } catch (err) {



public async getMetrics(sessionId: any, requestId: any): Promise <any> {

  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.

  • \$\begingroup\$ So this seems to be within the context of a class. You should include the entire class for review so full context can be seen. Also this appears to be Typescript, so you should ad that to your tags. \$\endgroup\$
    – Mike Brant
    Mar 18, 2019 at 21:33

1 Answer 1


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).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.