简体   繁体   中英

How to handle "Promise { <pending> }" in javascript

I have this function in javascript

const getData = async() => {

try {
    const serviceResponse = await someService("userId")
    return serviceResponse

  } catch (error) {
    console.log(error);
    
  }
}



const data = getData()
console.log(data)

Whenever I am running this I am getting this in console

 Promise { <pending> }

But when I am print data in the function itself I am getting the desired value

const getData = async() =>  {
 try {
     const serviceResponse = await someService("userId")
     console.log.(serviceResponse)

   } catch (error) {
     console.log(error);
    
  }
 }



 getData()

The following function is what I am importing from defined in another another

exports.someService = async(date,locationId,workerType) => {
    const value = await Attendance.find({})

    return value
}

Please can someone explain what is happening?

you need to await the promise from getData() too -

const data = await getData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM