Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda