Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda