Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Awaiting a void function in javascript

Hi I was wondering how I could await a void function.
I tried the following (mock up):

async function asyncer() {
  setTimeout(() => {
    console.log("async msg")
  }, 0)
}

async function helloer() {
  await asyncer()
  console.log("hello")
}

helloer()

This however doesn’t work, so I tried the following :

async function asyncer() {
  setTimeout(() => {
    console.log("async msg")
  }, 0)
  return true
}

async function helloer() {
  let ready = await asyncer()
  if (ready) {
    console.log("hello")
  }
}

helloer()

Still "hello" is still logged before "async msg".
Does anyone know a solution to this problem.
Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

async/await is a syntax sugar for promise.then, so create a Promise.resolve or smth to enter in async flow and then use async/await

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading