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

169
Views
Me resulta difícil entender por qué el código después de esperar no se ejecuta
async function setTime() { await new Promise(resolve => setTimeout(()=>{console.log("my func is fine")}, 5000)); } async function realFunction() { await setTime(); console.log("second call!"); }

En el código anterior, cuando llamo a realFunction, registra que mi función está bien, pero no registra la segunda llamada. ¿Puede alguien ayudarme a entender qué estoy haciendo mal y cómo puedo hacer que se registre la declaración?

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

0

Porque nunca resuelves tu promesa. Siempre es necesario resolverlo o rechazarlo.

También async / await await es bastante inútil para setTime

 function setTime(ms) { return new Promise((resolve) => setTimeout(resolve, ms)) } async function realFunction() { await setTime(5000); console.log("second call!"); }
about 4 years ago · Juan Pablo Isaza Report

0

El problema es que nunca resuelves la Promise por lo que queda pendiente para siempre.

Prueba a cambiarlo así:

 async function setTime() { await new Promise((resolve) => setTimeout(() => { console.log('my func is fine'); resolve(); }, 5000) ); } async function realFunction() { await setTime(); console.log('second call!'); }
about 4 years ago · Juan Pablo Isaza Report

0

Necesitas resolver la promesa, y para eso puedes llamar a resolve()

Entonces su función setTime() se vería así:

 async function setTime() { await new Promise(resolve => setTimeout(()=>{ console.log("my func is fine") return resolve(); }, 5000)); }
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!