javascript范围,perm无法获得正确的变量

时间:2016-09-28 08:20:51

标签: javascript

我无法从功能中获得烫发!! 这里有范围问题

var perm = "none";
    var participantsPromise = getChannelParticipants(peerID * -1).then(
        function (participants) {
          angular.forEach(participants, function (participant){
            if (participant.user_id == UserID) {
              switch (participant._) {
                case "channelParticipant":
                  perm = "normal";
                  console.log('->>>>>>> Perm = normal');
                  break;
                case "channelParticipantEditor":
                  perm = "Admin";
                  console.log('->>>>>>> Perm = Admin');
                  break;
                case "channelParticipantModerator":
                  perm = "Admin";
                  console.log('->>>>>>> Perm = Admin');
                  break;
                case "channelParticipantCreator":
                  perm = "Creator";
                  console.log('->>>>>>> Perm = Creator');
                  break;
                default:
                  console.log('#########> No handler for', participant._);
                  perm = "unknown";
              }
            }
          })
        });
    return perm;

函数返回none,但perm已设置为其他值。 我该怎么办?

2 个答案:

答案 0 :(得分:0)

试试这个

var perm = "none";
var participantsPromise = getChannelParticipants(peerID * -1).then(
function (participants) {
  if (participants.user_id == UserID) {
    switch (participants._) {
      case "channelParticipant":
        perm = "normal";
        break;
      case "channelParticipantEditor":
        perm = "Admin";
        break;
      case "channelParticipantModerator":
        perm = "Admin";
        break;
      case "channelParticipantCreator":
        perm = "Creator";
        break;
      default:
        console.log('No handler for', participants._);
        perm = "unknown";
    }
    // perm variable is normal :)
  } else {
    console.log('userid does not match');
    perm = "userId Error";
  }
});

答案 1 :(得分:0)

我认为问题在于,您在尝试同步读取perm的值时,异步回调中它已被更改。

var perm = "none"; // here value of `perm` is "none"

var participantsPromise = getChannelParticipants(peerID * -1).then(
    function(participants) {
        if (participants.user_id == UserID) {
            switch (participants._) {
                ...
                case "channelParticipantCreator":
                    perm = "Creator";
                    break;
            }
            // perm variable is normal :)
        }
    }); 
 console.log(perm) // perm is back to none - yes, 
// because the code `function(participants) ` hasn't yet 
// been executed, since it's asynchronous

// let's add a delayed callback that will wait until async code finishes executing
var timeToFinishGetChannelParticipants = 10000; // 10 seconds
setTimeout(function() {
      console.log(perm); // here it should have value set inside `function(participants)`
}, timeToFinishGetChannelParticipants);

<强>更新

function getPerm() {
    return getChannelParticipants(peerID * -1).then(
        function (participants) {
          if (participants.user_id == UserID) {
            switch (participants._) {
              case "channelParticipant":
                return "normal";
                break;
              case "channelParticipantEditor":
                return "Admin";
                break;
              case "channelParticipantModerator":
                return "Admin";
                break;
              case "channelParticipantCreator":
                return "Creator";
                break;
              default:
                console.log('No handler for', participants._);
                return "unknown";
            }
            // perm variable is normal :)
          } else {
            console.log('userid does not match');
            return "userId Error";
          }
        });
}

// outer code
getPerm().then(function(perm) {
    console.log(perm);
});