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

121
Vistas
Order of execution Javascript

I have this code:

setTimeout(function timeout() {
    console.log('timeout');
}, 0);

let p = new Promise(function(resolve, reject) {
    console.log('create promise');
    resolve();
});

p.then(function(){
    console.log('execute promise');
});

console.log('end');

I get this order when execute the code:

  • create promise
  • end
  • execute promise
  • timeout


Question: Why create promise is executed first? I expect to execute first end, because it is the only one synchrone code so it should be first executed.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The callback passed to the Promise constructor is executed immediately and synchronously. But it is possible to start an asynchronous task in a Promise callback. Then the Promise resolves after the asynchronous task has completed. This is the primary use-case of Promises.

In your case there is no asynchronous task started, so all your cody is synchronous.

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