Return value after JS promise finishes

I am working on a Discord bot and need to return a value after the reaction promise finishes.

function sendDM(userTag) {
  let sentMessage = undefined;
  let moderator = undefined;
  let status = "";

  try {
    client.users.fetch(userTag, false).then((user) => {
        user.send("User Authentication Request -\n```Discord Tag:" + userTag + "\n" + "Date Sent: " + getDate() + "\n" + "```" + "Authorize?").then((message => {
            moderator = user
            sentMessage = message
            message.react("✅");
            message.react("❌");
        }));
    })

    client.on("messageReactionAdd", (reaction, user) => {
      if (reaction.emoji.name == "✅") {
        const authorTag = client.users.cache.find(u => u.tag === moderator.tag);
        
        if (user.id  == authorTag) {
          status = "complete";
          console.log("Auth Request Complete");
          moderator.send("Authentication request was successful.");
        }
      } else {
        if (reaction.emoji.name == "❌") {
          const authorTag = client.users.cache.find(u => u.tag === moderator.tag);
        
          if (user.id  == authorTag) {
            status = "dropped";
            console.log("Auth Request Dropped: User Denied");
            moderator.send("Authentication request was dropped.");
          }
        }
      }
    }).then(someShit => console.log("test: " + someShit))
  } catch(error) {
    console.log("DM ERROR_" + error);
  }

// Return would be here
}

The problem is once the by the time the user reacts, the function has already returned the empty "status" string.

66 thoughts on “Return value after JS promise finishes”

Leave a Comment