Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda