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

327
Vistas
Despite Await the function is being executed instantly

I am new to Javascript and am facing trouble dealing with Async/Await. Here is my code, as per my understanding adding await puts on hold the function Here, inside foo, it should put on hold the code at checker() and move to running the commands outside foo, but instead it executes the checker() function before console.log('the end').

function checker() {
    console.log('inside checker');
    return "Ok"
}

async function foo() {
    let a = await checker(); // Shouldn't using await here let next line outside foo to execute
    console.log("mid", a);
}
console.log(1);
foo();
console.log('the end');
// Output coming is: 
//1
//inside checker
//the end
//mid Ok

Can someone please tell me what properties are making it to behave this way. I know there's something that I am missing but can't figure out what.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your checker function is not asynchronous, but you also need to do something that is actually async within it.

At the moment, all of the promises are instantly resolved which provides no opportunity for later code to run.

We need to actually create a promise, merely defining a function as async isn't enough unless something inside is returning a promise, so we return a new promise which is resolved by the setTimeout callback.

Note that the timeout period is 0 which is enough to cause the javascript event loop to be invoked, which allows the next statement(s) after calling foo to be processed. Changing this to any positive integer will change the execution time but not the order of events.

function checker() {
    return new Promise(resolve => {
        setTimeout(() => resolve('Ok'), 0)
    })
}

function foo() {
    let a = checker();
    console.log(new Date(), "mid");
    return a
}

(async () => {
    console.log(new Date(), 'start');
    foo().then(v => console.log(new Date(), 'eventually', v));
    console.log(new Date(), 'the end');
})()

about 4 years ago · Juan Pablo Isaza Denunciar

0

You didn't define your function as async

function checker() { console.log('inside checker'); return "Ok" }

It should be

async function checker() { console.log('inside checker'); return "Ok" }

about 4 years ago · Juan Pablo Isaza Denunciar

0

await tells your code to wait for the function to finish. If you want to continue running without waiting, then you shouldn't use the await keyword. async also needs to be on the function that you want to run asynchronously, in this case checker()

async function checker() {
    console.log('inside checker');
    return "Ok"
}

function foo() {
    let a = checker(); 
    console.log("mid", a);
}
console.log(1);
foo();
console.log('the end');
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