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

76
Views
For loop skipping over await sleep

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?

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

0

const sleep = (millis) => new Promise((resolve) => setTimeout(resolve("done"), millis));

calls resolve("done") immediately, and passes the result of the call resolve("done") as parameter to setTimeout.

You either pass the function resolve as parameter to setTimeout (there is not real need to resolve with done):

const sleep = (millis) => new Promise((resolve) => setTimeout(resolve, millis));

Or if you really want to resolve with done use an arrow function:

const sleep = (millis) => new Promise((resolve) => setTimeout(() => resolve("done"), millis));
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!