I am having an issue where i have a function that should sleep for a designated time if data is not populated in an array, before moving on to the next iteration.
For whatever reason the sleep is getting skipped over and it just continues without sleeping.
const sleep = (millis) => new Promise((resolve) => setTimeout(resolve("done"), millis));
async function getBackupData(ID, Data) {
const sleeptime = 60;
const attempts = 10;
let urlArray;
for (let i = 0; i < attempts + 1; i += 1) {
if (i === attempts) { throw new Error("Download URL not available"); }
urlArray = await getDownloadUrls(ID, data);
if (urlArray.length > 0) {
break;
}
console.log(`${i} - Download url not available yet: sleeping ${sleeptime} seconds`);
await sleep(sleeptime * 1000);
}
console.log(`returning ${urlArray[0]}`);
return urlArray[0];
}
output:
0 - Download url not available yet: sleeping 60 seconds
1 - Download url not available yet: sleeping 60 seconds
2 - Download url not available yet: sleeping 60 seconds
3 - Download url not available yet: sleeping 60 seconds
4 - Download url not available yet: sleeping 60 seconds
5 - Download url not available yet: sleeping 60 seconds
6 - Download url not available yet: sleeping 60 seconds
7 - Download url not available yet: sleeping 60 seconds
8 - Download url not available yet: sleeping 60 seconds
9 - Download url not available yet: sleeping 60 seconds
Unhandled Rejection at: Promise Promise {
<rejected> Error: Download URL not available
This should be sleeping for 60 seconds on each iteration but this takes only a few seconds to complete as is... Can someone please take a look to see what is going on?