2

我试图了解我真正在做什么,因为我觉得我缺乏一些东西。您能否指出我的某个地方或确认我的错误/理解?

request.then(function(response) {
        updateCurrentUser(response.data.data);
        currentUser.isAuthenticated();
      });

基本上是这样吗?

request = {
    then : function (foo){
        foo("first")
    } }

request.then(function (response) { console.log(response) ; });

如果你在这里看到完整的代码# 35和#63

指示:

    AuthenticationService.login($scope.user.email, $scope.user.password).then(function(loggedIn) {
      if ( !loggedIn ) {
        $scope.authError = "Login failed.  Please check your credentials and try again.";
      }
    });

AuthenticationService 作为工厂:

login: function(email, password) {
  var request = $http.post('http://', {email: email, password: password});
  return request.then(function(response) {
    updateCurrentUser(response.data.data);
    return currentUser.isAuthenticated();
  });

},

我不明白的是,loggedIn 变量的值为什么等于语句返回 currentUser.isAuthenticated();的值。返回AND 不等于原始的 then(function(response),因为我从 AuthenticationService 返回承诺。关于上面的示例,如何实现这一点?

谢谢你。

4

1 回答 1

2

我认为概念的问题源于您忽略了return声明的事实。AuthenticationService.login实际上是一个带有预定义请求的闭包,所以你可以想象它被login替换为它的返回值request.then(function(response) {...。然后您可以简单地推断出整个代码行是:

AuthenticationService.login($scope.user.email, $scope.user.password).then(
function(response)
{
    updateCurrentUser(response.data.data);
    return currentUser.isAuthenticated();
}).then(
function(loggedIn)
{
  ...

这样,您可能会看到响应的结果应该作为登录检查下一步的输入出现。

于 2012-12-28T15:29:55.883 回答