需要第二双眼睛……发现这有什么问题吗?我的内部函数没有返回值。我一定是把示波器搞砸了?
function getGroups(account){
var name;
// name = 'assigned'; I work
account.getGroups(function(err, groups) {
//removed logic for simple debugging
name ='test';
//return name;
});
return name;
}
在父函数中分配变量时,即 var name = 'assigned'
它有效。
您的 account.getGroups
可能是一个异步函数,它需要一个 callback
函数。这个回调
函数,
function(err, groups) {
//removed logic for simple debugging
name ='test';
//return name;
}
不会立即执行。因此,您的 return name;
语句会在您的 name = 'test';
语句之前执行。
希望这是有道理的。
要从回调中获取更新值,您必须使
getGroups
异步或基于事件
制作异步很容易
function getGroups(account, callback){
var name;
// name = 'assigned'; I work
account.getGroups(function(err, groups) {
//removed logic for simple debugging
name ='test';
callback(name);
});
}
而不是为值调用函数(例如,'var groupname = getGroups(account)')你必须像下面那样做
getGroup(account, function (groupname){
// do whatever you like with the groupname here inside this function
})