Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda