Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!