我无法在javascript中将用户的电子邮件地址存储在变量中。我想要做的是调用API,获取电子邮件地址,并将其存储在“email”变量中,以便在屏幕上的其他功能中使用。
我的代码如下所示:
function emailCheck(){
var email;
FB.api('/me',email = function(response){
return response.email;
})
}
alert(email);
我确实检查过以确保我拥有适当的权限。我会将alert(response.email)
放在我的回复部分,它会通过正确的电子邮件提醒。问题是我无法通过函数 访问电子邮件变量。
答案 0 :(得分:1)
您需要使用延续,因为所有API方法都是异步的
function emailCheck(continuation){
FB.api('/me', function(response){
continuation(response && response.email);
});
}
emailCheck(function(email) {
alert(email);
}
另一种方法是使用Promises,Futures或deferreds(实际上是同样的事情)来抽象它。