1

我无法将用户的电子邮件地址存储在 javascript 的变量中。我要做的是调用 API,获取电子邮件地址,并将其存储在“电子邮件”变量中,以用于屏幕上的其他功能。

我的代码如下所示:

function emailCheck(){
    var email;
    FB.api('/me',email = function(response){
        return response.email;
        })
    }

    alert(email);

我确实检查以确保我拥有适当的权限。我会放入alert(response.email)我的回复部分,它会通过正确的电子邮件发出警报。问题是我无法在函数之外访问电子邮件变量。

4

1 回答 1

1

您需要使用延续,因为所有 API 方法都是异步的

function emailCheck(continuation){
  FB.api('/me', function(response){
    continuation(response && response.email);
  });
}

emailCheck(function(email) {
  alert(email);
}

另一种方法是使用 Promises、Futures 或 deferreds(实际上是一样的)来抽象其中的一部分。

于 2012-12-21T02:54:42.823 回答