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);
}
})();