Search code examples
node.jspromiseasync-awaites6-promiseazure-function-app

How to wait for the promise to get resolved


I am trying to write a function inside a async function in nodejs. here is my sample script

module.exports = async function (context, req) {
   var mydata = (async () => {
     return "output needed"
   }) ()
  
   console.log(mydata)
}

Expected Output is : output needed

What I get is : promise{ <pending> }

Any Idea to wait till the promise is fulfilled?


Solution

  • You need to await the Promise returned by mydata:

    async function test(context, req) {
       var mydata = (async () => {
         return "output needed"
       }) ()
      
       console.log(await mydata)
    }
    
    test()