Anon
Anon

Reputation: 59

Use value inside an [Object Promise]

I'm having a issue trying to use a value that I get doing a call to an API.

Here is my Typescript code:

 private async getPersonName() {
            let fullName = await Api.GetName('Data($select=FullName)', `PersonId  eq ${currentPersonId}`);
            return fullName.value[0].Data.FullName;
        }

And then, I need to just append it to the DOM using Jquery.

$("#myId").text(/*getPersonName() value*/);

When I try to append this it show [object Promise] in the DOM. I'm not really sure how to properly use this value.

Regards.

Upvotes: 2

Views: 599

Answers (2)

phuzi
phuzi

Reputation: 13079

Since getPersonName() is declared async, you should probably await the call to it.

var value = await getPersonName();
$("#myId").text(value);

or even

$("#myId").text(await getPersonName());

Upvotes: 0

dorintufar
dorintufar

Reputation: 650

Try this $("#myId").text(await getPersonName());
Or this:

private async getPersonName() {
   let fullName = await Api.GetName('Data($select=FullName)', `PersonId  eq ${currentPersonId}`);
   return fullName.value[0].Data.FullName;
}

...

getPersonName().then((value) => {
    $("#myId").text(value);
})

Upvotes: 2

Related Questions