0

I require the user details for multiple areas. I tried to return the details using a generic function. But i am getting the result as undefined. i understand that, I require to use the $differed to get the reuslt. But I don't have any idea about my current scenario.

anyone help me here please?

here is my function:

$scope.currentUserInfo = function () {

        var userDetails;

        server.getProfile.get().$promise.then(function ( response ) {

            if( response.Message.toUpperCase().indexOf('SUCCESS') != -1) {

                return  userDetails = response;

            }

            return "Not able to get the user details this time"

        })

        return userDetails;

    }

$scope.currentUser =  $scope.currentUserInfo();
console.log( $scope.currentUser ) //undefined.



var function1 = function () {

    $scope.currentUserInfo(); //once user details available do further

     $scope.age = $scope.currentUser.age;

}

var function2 = function () {

    $scope.currentUserInfo(); //once user details available do further

    $scope.name = $scope.currentUser.name;

}
1

1 Answer 1

1

server.getProfile.get() is an asynchronous call. The return userDetails; line in $scope.currentUserInfo function will get executed even if the server.getProfile.get() call is not yet finish. Try this:

$scope.currentUserInfo = function () {

    server.getProfile.get().$promise.then(function ( response ) {

        if( response.Message.toUpperCase().indexOf('SUCCESS') != -1) {

            $scope.currentUser = response;

        }

        $scope.message =  "Not able to get the user details this time";


    })

}

$scope.currentUser will be populated after the ajax call is finish. I assume that you are using $scope.currentUser in your html bindings so when the value changes it will automatically reflect in your view

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

3 Comments

I agree. But Up to I receive the value from the $scope.currentUser I would like to pause other functions - how to do?
What do you mean by pause other functions?
how or where do you use function1 and function2? if for instance you want to do something when $scope.currentUser changes, you may want to watch $scope.currentUser

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.