Why is my data turning back into a promise?

I’m using requestConfluence in my Custom UI app to convert an array of user ids to names. I have the following:

const getNames = (ids) => {
    const namesList = ids.map(async (id) => {
      const profile = await requestConfluence(
        `/wiki/rest/api/user?accountId=${id}`
      );
      const user = await profile.json();
      console.log(user.publicName);
      return user.publicName;
    });
    console.log(namesList);
}

So this should take an array of ids, and for each one it gets the user’s profile information and then returns the publicName field. This creates an array of names, which I then log to the console.

The line console.log(user.publicName); correctly prints the public name to the console, but the line console.log(namesList); prints a promise instead.

I don’t understand why this is happening - can anyone help?

Your async method inside map will return a promise. The async keyword “Promisifies” your function. Meaning that it actually returns a promise that resolves to what you imperatively return in your code.

Your code is effectively mapping your array of ids to an array of promises. You will need to resolve the promises to get to the actual names in the manner you are trying.

Adding this to the end of the above block will probably produce the effect you want:

Promise.all(namesList).then((resolvedNamesList)=>{
  console.log(resolvedNamesList);
});
2 Likes

Thank you!