Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda