Home > Back-end >  Async function not returning value, but promise
Async function not returning value, but promise

Time:09-27

I have an async function: async function getUrl() { let url = await ngrok.connect(3000) return url }. By my understanding, an async function waits for the promise to be fulfilled, and return the value. Inside the function, url is a string, but when I call the function, it returns a promise.

CodePudding user response:

async function myPromiseFunction() {
  return Promise.resolve('hi')
}

(async () => {
  console.log(await myPromiseFunction());
})()

That how it works, you need to use await when you call your promise function or use .then/.catch(I recommend not to, and learn async / await)

  • Related