2016-09-28 71 views
0

我不能从功能获得烫发! 这里存在范围问题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; 

函数返回无,但烫发已经设置其他值。 我能做什么?

+0

请参阅[我的回答(http://stackoverflow.com/a/39742108/2545680) –

回答

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"; 
    } 
}); 
+0

有与处置 和烫发没有问题的内部变化这个开关 但是外面?它返回到没有! –

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); 

UPDATE:

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); 
}); 
+0

setTimeout是好的,但我不能返回任何东西 这整个代码是在一些函数 ,它必须返回该perm值 –

+0

@pouyarajaei,你只能返回一个承诺和外部代码应该等到承诺解决。看到我的更新 –