Saurabh
Saurabh

Reputation: 1662

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 ?

Upvotes: 1

Views: 53

Answers (1)

andy mccullough
andy mccullough

Reputation: 9571

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

const data = await getData()

Upvotes: 2

Related Questions