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

361
Views
Why my async / await function is not working properly

I have next doubt, I was practicing about async/await functions stuff. I have created a simple test:

console.log('Testing async-await');

main();

async function wait(nSeconds) {
  await setTimeout(function() {
    console.log('Request finished !!');
  }, nSeconds * 1000);
}
async function main() {
  let a = await wait(5);
  console.log('end !!');

}

My idea, as long as I understand is simulate with wait(nSecond) function a waiting like a ajax request, for example. Everithing is called in main() function.

My actual output is :

enter image description here

But... souldnt be ??? :

Testing async-await
Request finished !!
end !!

Im telling to my code, when you get on line 'let a = await wait(5);' just dont continue until end of function is resolved . So what is happening here.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The function wait should look like this:

function wait(nSeconds){
    return new Promise( resolve => setTimeout( resolve, nSeconds * 1000 ) )
}

The setTimeout method does not return promise so await keyword does not work in this case.

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!