为什么在下面的变量x中返回未定义?
const test = function() {
let x;
getAccessToken(function(callback){
getUserPrincipals(callback.accessToken, function(getUserPrincipalsResponse){
x = getUserPrincipalsResponse;
console.log(`this is from getUserPrincipals \n ${x}`);
})
})
console.log(x);
return x;
}
console.log(test());
控制台记录以下内容将返回值:
console.log(`this is from getUserPrincipals \n ${x}`);
但是控制台记录x返回未定义:
console.log(x);
我在做什么错或不了解变量范围?我认为块代码中的变量有效吗?很感谢任何形式的帮助。我是新手。谢谢。