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

137
Visualizações
an example of async/await in javascript

I am studying async and await in javascript, and a little bit confused about the execution order of the following function I wrote when I play around async and await:

const promiseA = new Promise((resolutionFunc, rejectionFunc) => {
  setTimeout(() => resolutionFunc(666), 5000);
});

const test = async () => {
  await promiseA.then((val) =>
    console.log("asynchronous logging has val:", val)
  );
};

const testTwo = () => {
  test();
  console.log("immediate logging");
};

testTwo();

my understand is that in function <testTwo()>, <test()> execute first, it's been called & executed, <test()> is an async function, and I have put the await in front of the promise.

why this console.log("immediate logging"); line of code get executed first?

on the other hand, if I wrote <testTwo()> like following:

const testTwo = async () => {
  await test();
  console.log("immediate logging");
};

everything is fine

about 4 years ago · Juan Pablo Isaza
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