2

I am trying to understand what i am really doing, since i feel i am lack of something. Could you please point me somewhere or confirm my mis/understanding?

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

Is basically this?

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

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

If you see full code here#35 and here#63

directive:

    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 as factory:

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();
  });

},

The thing i don't understand is how come that the value of loggedIn variable is equal to the value what statement return currentUser.isAuthenticated(); returning AND NOT equal to the then(function(response) of original as i am returning promise from AuthenticationService. And how this could be accomplished regarding to the examples above?

Thank you.

4
  • It's not clear what it is that you're asking here. Commented Dec 28, 2012 at 14:29
  • i will try to reformulate, thank you Commented Dec 28, 2012 at 14:34
  • Are you asking how a variable foo can be passed to .then(function(foo) {? For such functionality you'll need a little more advanced function: jsfiddle.net/XaYXY. Commented Dec 28, 2012 at 15:16
  • hmm..not. In request.then(function(response) .. in response have lets say value "A". Then i return that request and do again request.then(function(loggedIn) and now in loggedIn have value "B" ..regarding to the what currentUser.isAuthenticated() returning...how come? Commented Dec 28, 2012 at 15:46

1 Answer 1

2

I think the problem with conception arises from the fact that you overlooked the return statement. What AuthenticationService.login does is actually a closure with predefined request, so you can imagine that login is replaced with its return value request.then(function(response) {.... Then you can simply deduce that entire code row is:

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

This way you may see that result from response should occur as input for the next step with login check.

Sign up to request clarification or add additional context in comments.

2 Comments

heck, yeah maybe .. but i still don't get it this way. Could you please be more verbose?
hell, yeah i got it : Because calling then api of a promise returns a new derived promise, it is easily possible to create a chain of promises: and following works: $http.get('http://').then(function(greet) { console.log(greet); return "something else" }).then(function(greet2){console.log(greet2);});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.