2016-09-28 48 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,你只能返回一個承諾和外部代碼應該等到承諾解決。看到我的更新 –