Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
How to return a string from an async function

Please note: I m posting this question becuase I could not find a soultion to my problem from similar other questions. Any help in this regard would be gretaly appreciated.

I have a function:

const getUsenameProps =  (id: string) => {
    const userNameProps: IUserName = {
    friendName : getFriendName(id).toString(),
    friendId : id
    }
    console.log("AAAAAAAAAAAAA", userNameProps) //{friendName: Promise<string>, friendId: 1}
    return userNameProps
}

and the called function is like this

export const getFriendName =  async (id: string) => {
    const friendContact = await getUserData(arrayArgument,id)
    const friendName = friendContact && friendContact .id !== "" ?  friendContact .name : ""
    console.log("friendame") //sam
    return friendName 
}

Now, when I call the getUsernameProps(), it inturn calls the getFriendName() and this makes an API call. The console.log in getFriendName() prints a normal string ("sam"), and I am returning this. But when this is called in getUserNameProps, the value it is getting back is a Promise. I want to set userNameProps.friendName to the friendName value ("sam") returned in fetFriendName.

Please help

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!