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

198
Vistas
What happens exactly when we call .then after a Promise is resolved?

I have a var to keep the promise object, and call .then in different places. I got the same timestamp for different .then. What happens when call .then to a already resolved promise object?

var promise1 = new Promise((resolve) => {resolve(Date.now().toString())})

promise1.then((response) => {console.log(response)});
promise1.then((response) => {console.log(response)});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What happens exactly when we call .then after a Promise is resolved?

It schedules (right before the next tick of the event loop) for the .then() handlers to get called.

So, the current Javascript that is executing will continue to execute and finish and when it returns control back to the event loop, then the .then() handlers that were just registered on an already-resolved promise will call their callbacks in the order they were registered. Promise handlers are served before other things in the event loop so it doesn't actually go all the way into the event loop until there are no more promise callbacks to call.

So, it is always safe to register a .then() handler. You don't have to worry about whether the promise is already resolved or not. Either way, the .then() handler will get called (assuming the promise resolves or was already resolved).

So, if you modify the logging a bit like this:

const promise1 = new Promise((resolve) => {resolve(Date.now().toString())})

promise1.then((response) => {console.log("first .then()")});
promise1.then((response) => {console.log("second .then()")});
console.log("now here");

You will get this output:

now here
first .then()
second .then()
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