Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
What could be a better option to call recursive or better method a JS function which has to retry a request if a value is null

I built a function in my NodeJS backend which has that purpose to retry for 10 times a request if a value on first request is equal to null. This behavior is need it because there is a worker function which uploads PDF to AWS, so the value I'm retrieving by the new function contains a PDF URL which very often on first request is null.

The goal is to check at first if we have the pdfUrl if that is not NULL so return the data.

If the pdfURLfor some reason is NULL then the function need to retry the request with a timeout of 20s and max 10 retries.

The function I tried is as follow and have several issues:

  1. Seems is retrying 10 times either if the pdfUrl is not null instead of retrying only if it is null and only for max retries
  2. The function after the 10 times should give back an error exception and not null
  3. The function should give up and giving the error only if fails for 10 times
async function getIcfDocument(documentId, { log }) {
  log.info('Getting IcfDocument with last revision pdfUrl');
  const MAX_RETRIES = 10;
  const timeout = 20000;

  let data;
  for (let i = 0; i <= MAX_RETRIES; i += 1) {
    try {
      const { icfDocuments } = await request(
        CONSENT_SERVICE_URL,
        getICFDocumentRecipientsQuery,
        {
          documentId,
        }
      );

      if (icfDocuments.nodes[0].revision.pdfUrl) {
        log.info('Success the revision pdfUrl is present');
        data = icfDocuments;
      } else {
        log.debug(
          'Waiting to retrieve the last revision pdfUrl %s ms',
          timeout
        );
        await sleep(timeout);
        log.debug('Retrying times %s', i);
      }
    } catch (err) {
      log.error(
        'Failed to retrieve the data for document %s, Error: %s',
        documentId,
        err.message
      );
      return null;
    }
  }
  return data;
}

Will be nice to see a better way to what I have tried as I have some difficulties to make it so.

The request method above is requesting to a GQL query that why was implemented in this way

const { icfDocuments } = await request(
            CONSENT_SERVICE_URL,
            getICFDocumentRecipientsQuery,
            {
              documentId,
            }
          );

The icfDocuments is the resulting OBJ of the query which contains the urlPdfand other information. This query has to be retried if that url is null for max 10 times and 20s timeout between requests.

The query is this one as info

const getICFDocumentRecipientsQuery = gql`
  query GetICFDocumentRecipients($documentId: ID!) {
    icfDocuments(filter: { ids: [$documentId] }, pagination: { limit: 1 }) {
      nodes {
        id
        recipients {
          id
          email
          phone
          locale
        }
        revision {
          pdfUrl
        }
      }
    }
  }
`;


 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Return right away when the data is found without continuing the loop.
  • If the pdf url is not found, show log error instead of retry message.
async function getIcfDocument(documentId, { log }) {
  log.info('Getting IcfDocument with last revision pdfUrl');
  const MAX_RETRIES = 10;
  const timeout = 20000;

  for (let i = 0; i <= MAX_RETRIES; i++) {
    try {
      const { icfDocuments } = await request(
        CONSENT_SERVICE_URL,
        getICFDocumentRecipientsQuery,
        {
          documentId,
        }
      );

      if (icfDocuments.nodes[0].revision.pdfUrl) {
        log.info('Success the revision pdfUrl is present');
        return data; // SOLUTION TO #1
      } else {
        if (i == MAX_RETRIES) return log.error('Failed to retrieve pdfUrl after %s tries.', MAX_RETRIES); // SOLUTION TO #2
        log.debug(
          'Waiting to retrieve the last revision pdfUrl %s ms',
          timeout
        );
        await sleep(timeout);
        log.debug('Retrying times %s', i);
      }
    } catch (err) {
      log.error(
        'Failed to retrieve the data for document %s, Error: %s',
        documentId,
        err.message
      );
      break;
    }
  }
  return null;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!