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

70
Visualizações
how console.log1234 in synchronous way

How can I console 1234 in this function. using await or async. Now it logs 1243. It should wait before the 3 then log the 4.

    function Call(){
     console.log('1');
     console.log('2');
     setTimeout(()=>console.log('3'),1000);
     console.log('4');
    }
    Call();

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

0

As suggested in the comments, the key here is to create a promise-based setTimeout as described here:

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

Then make your function an async function and await the timeout:

async function call(){
    console.log('1');
    console.log('2');
    await delay(1000);
    console.log('3');
    console.log('4');
}
call();

Normally, you want to handle errors, but we know the above can't throw any, so...

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

async function call(){
    console.log('1');
    console.log('2');
    await delay(1000);
    console.log('3');
    console.log('4');
}
call();

how console.log1234 in synchronous way

Note that the above makes the logic in the function synchronous, but the code does not execute synchronously. call returns as of the await, and then resumes later when the timer fires and settles the promise.

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