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

127
Views
JavaScript no espera la promesa de resolver

En el siguiente código, lo que busco es recibir la alerta, después de que finalice el conteo en setInterval. Sin embargo, la alerta se muestra primero y luego ocurre el conteo. ¿Qué he hecho mal?

 function function1() { return new Promise((resolve, reject) => { console.log('in function1 ') function2().then(resolve()); }); } function function2() { return new Promise((resolve, reject) => { var x = 0; I = setInterval(function() { console.log(x); if (x > 10) { clearInterval(I); resolve() }; x++ }, 100); }); } function1().then(alert('finished'));

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

0

El primer argumento de Promise#then :

onFulfilled : una función llamada si se cumple la promesa. Esta función tiene un argumento, el valor de cumplimiento. Si no es una función, se reemplaza internamente con una función de "Identidad" (devuelve el argumento recibido).

Puede pasar un nombre de función o una función como se muestra a continuación:

 function function1() { return new Promise((resolve, reject) => { console.log('in function1 ') function2().then(resolve); // onFulfilled }); } function function2() { return new Promise((resolve, reject) => { var x = 0; I = setInterval(function() { console.log(x); if (x > 10) { clearInterval(I); resolve() }; x++ }, 100); }); } function1().then(() => alert('finished')); // onFulfilled

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!