Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

node "promise.all" "retry" site:stackoverflow.com

async function wait(timeInMs) {
  console.log('Waiting ...');
  return new Promise((resolve => setTimeout(() => resolve(), timeInMs)));
}


async function callExternalApi(numberOfTries, timeout) {
  if (numberOfTries <= 0) {
    return "Data not available in API";
  }

  const result = await myapiCall();

  if (result) {
    return result;
  }

  await wait(timeout); // wait for the defined timeout before recurring

  return callExternalApi(numberOfTries - 1, timeout);
}

(async () => {
   try {
     const result = await callExternalApi(3, 1000);
     console.log(result);
   } catch(err) {
     console.log(err);
   }
})();
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #node
ADD COMMENT
Topic
Name
9+3 =