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

179
Visualizações
Why promise fires function immediately?

I can't get why function in new Promise fires immediately. Shouldn't it be put in Microtask queue and fire after all global code has executed?

Code:

const prom = new Promise((resolve, reject) => {
  console.log("in Promise");
  resolve(20);
});
prom.then((data) => {
  console.log(12312323);
  console.log(data);
});

console.log(77);

Link to code

Output:

in Promise 
77
12312323
20
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you construct a Promise, its task begins execution immediately.

const p = new Promise((resolve) => {
  console.log('in promise');
  resolve("value");
})

Promises are primarily useful for asynchronous tasks like network requests, and their delayed resolution can make it seem like the execution itself has been deferred, but that's not what's happening.

In this example you can see that there can be a significant delay between when the task starts and when the promise is resolved:

const p = new Promise((resolve) => {
  console.log('first: in promise'); // first
  setTimeout(() => resolve('resolved value'), 2000) // wait 2 seconds before resolving.
})

p.then(val => console.log(`third: ${val}`)); // third

console.log("second: after p.then"); // second

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