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

83
Views
Para saltar el bucle esperar dormir

Tengo un problema en el que tengo una función que debería dormir durante un tiempo designado si los datos no se completan en una matriz, antes de pasar a la siguiente iteración.

Por alguna razón, el sueño se salta y continúa sin dormir.

 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]; }

producción:

 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

Esto debería estar durmiendo durante 60 segundos en cada iteración, pero esto toma solo unos segundos para completarse como está ... ¿Alguien puede echar un vistazo para ver qué está pasando?

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

0

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

llama a resolve("done") inmediatamente y pasa el resultado de la llamada resolve("done") como parámetro a setTimeout .

Puede pasar la función resolve como parámetro a setTimeout (no hay una necesidad real de resolver con done ):

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

O si realmente desea done con el uso de una función de flecha:

 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!