[英]Trying to get the JavaScript variable scope correct in this trello.get code
[英]javascript scope, perm cant get correct variable
我不能从功能烫发! 这里有范围问题
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;
该函数不返回任何值,但perm已在其他值上设置。 我能做什么 ?
试试这个
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";
}
});
我相信问题在于,您正在尝试同步读取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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.