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

171
Visualizações
I am finding it hard to understand why the code after await does not execute
async function setTime() {
    await new Promise(resolve => setTimeout(()=>{console.log("my func is fine")}, 5000));
}

async function realFunction() {
    await setTime();
    console.log("second call!");
}

In the above code, when i call realFunction, it logs my func is fine, but it does not log second call. Can someone help me understand what am I doing wrong, and how can I get the statement to be logged?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Because you never resolver your promise. You always need to resolve or reject it.

Also async / await is pretty much useless for setTime

function setTime(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}

async function realFunction() {
    await setTime(5000);
    console.log("second call!");
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The prolbem is that you never resolve the Promise so it's pending forever.

Try to change it like this:

async function setTime() {
  await new Promise((resolve) =>
    setTimeout(() => {
      console.log('my func is fine');
      resolve();
    }, 5000)
  );
}

async function realFunction() {
  await setTime();
  console.log('second call!');
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to resolve the promise, and for that you can call resolve()

So your setTime() function would look like this:

async function setTime() {
    await new Promise(resolve => setTimeout(()=>{
        console.log("my func is fine")
        return resolve();
    }, 5000));
}
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