获取 forEach 循环中已定义变量的值

Get value of defined variable which is in forEach loop

var rank = "yeah";
var one, two, three, four;

Trello.board.searchCards('Myboardidhere').then(function(response) {
  for (let i = 0; i < response.length; i++) {
    const element = response[i];
    if (element.name === m.content) {
      var idList = element.idList;
      if (idList === sdLid) {
        rank = "Sheriff Deputy";
      } else if (idList === dfcLid) {
        rank = "Deputy First Class";
      } else if (idList === cplLid) {
        rank = "Corporal";
      } else if (idList === sgtLid) {
        rank = "Sergeant";
      } else if (idList === ltLid) {
        rank = "Lieutenant";
      } else if (idList === cptLid) {
        rank = "Captain";
      } else if (idList === devLid) {
        rank = "Developer";
      } else if (idList === mjrLid) {
        rank = "Major";
      } else if (idList === hdevLid) {
        rank = "Head Developer";
      } else if (idList === asLid) {
        rank = "Assistant Sheriff";
      } else if (idList === usLid) {
        rank = "Undersheriff";
      } else if (idList === SLid) {
        rank = "Sheriff";
      } else {
        rank = "Citizen";
      }
      var one = rank;
      rank = one;
      console.log(one + " - one");
    }
    var test = one;
    console.log(test + " - test");
  }
  var test2 = test;
  var anotherTest = 12;
  console.log(test2 + " - test2");
});
console.log(anotherTest);

console.log(rank + " - rank");

//CHANGE USERNAME
message.member.setNickname(m.content + " | " + rank) // Sets the users nickname

您好。所以我目前正在使用 node.js (discord JS trelloapi)。好吧,实际上没有必要准确解释我想做什么,但我正在尝试获取 for(;;) 循环和块中的 rank 变量的值. (找了很多,找不到答案)

利用异步和等待。 anotherTest 在一个承诺中,它是异步的。 这意味着该值仅在您 console.log 之后设置。 如果您使用 await,那么此代码将像同步一样运行。