-2

I know this is a very very dumb question, but I'm very tired and just cant seem to come up with an idea to fix this:

            const fetched = filteredResult.sort((a, b) => b.data - a.data).slice(0, 10)
            const fetchUser = async id => client.users.fetch(id);
            message.channel.send(
                new Discord.MessageEmbed()
                    .setTitle("Pp Leaderboard")
                    .setColor(roleColor === "#000000" ? "#ffffff" : roleColor)
                    .setDescription(
                        fetched.map((v, i) => {
                            v.ID = v.ID.replace("globalMessages_", "");

                            return `#${i + 1} **${fetchUser(v.ID).then(user => { user.tag })}** with ${commaNumber(v.data)} thanks\n`;
                        })
                    )
            )

This is a thanks leaderboard command ^^^. It shows top ten global rankings.

Don't worry about the other stuff - only about fetchUser(v.ID)... Once I do the command it returns an embed with the text and then in the place with fetchUser it shows [Object promise].

I want it somehow to return the tag of that discord user in that place. Thank you in advance!

deadkill
  • 13
  • 3
  • Does this answer your question? [How to get data returned from fetch() promise?](https://stackoverflow.com/questions/47604040/how-to-get-data-returned-from-fetch-promise) – HoldOffHunger Sep 06 '21 at 12:46

1 Answers1

0
const fetchUser = async id => client.users.fetch(id);

fetchUser is an async arrow function. Asynchronous functions always return a promise. To get the value you have to await fetchUser(...).

.setDescription(
  // Changed the `map()` callback to an async function so we can use
  // `await`. This will return an array of promises, so we need to wrap
  // the `map()` result with `Promise.all()`.
  await Promise.all(fetched.map(async (v, i) => {
    v.ID = v.ID.replace("globalMessages_", "");
    return `#${i + 1} **${(await fetchUser(v.ID)).tag}** with ${commaNumber(v.data)} thanks\n`;
  }))
)

This assumes that you can use await in the current context of your program.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52