1

I've got 2 factory functions:

Factories

factory.getCurrEmployee = function()
{
    data = {"api_token": authenticationFactory.getToken()};
    url = GLOBALS.url + 'show/employee/' + $cookieStore.get('employeeid');
    return requestFactory.post(url, data)
        .then(function (response) {
            return response.data.result.Employee;
        }, function () {
            $window.location.assign('/');
        });
}

factory.isSuperadministrator = function() {
    factory.getCurrEmployee().then(function (employee) {
        if(employee.Role == 'Superadministrator')
        {
            return true; //console.log('Superadministrator')    <- that console.log is visible in my console
        }
        return false;
    });
}
return factory;

In my controller I would expect true or false (a user is a Superadministrator or not) but the result is nothing. If I console.log in my factory.isSuperadministrator the result is true.

Controller

console.log(employeeFactory.isSuperadministrator());

Why is this not working?

4
  • Its async. Your returning promises not a bool value. Commented Mar 24, 2016 at 15:25
  • @ste2425 those Boolean value's will never get returned untill, promise gets returned. because boolean has been return from callback function. Commented Mar 24, 2016 at 15:30
  • @PankajParkar Yes, but the fact the OP has no use of .then in his calling controller code but is returning the expected value in the service callback shows an underlying lack of knowledge on how promise chaining works. Which is why i said he's returning a promise not the value in his callback. Commented Mar 24, 2016 at 15:33
  • @ste2425 apology.. I misunderstood it. though cheers..I think, OP got what he wanted :-) Commented Mar 24, 2016 at 15:36

2 Answers 2

4

Missed to return promise from factory.getCurrEmployee in factory.isSuperadministrator function. Also I did small refactoring which makes your code more smarter.

factory.isSuperadministrator = function() {
    return factory.getCurrEmployee().then(function (employee) {
        return employee.Role == 'Superadministrator';
    });
}

But above thing doesn't solve your problem, it will only print promise object in console. To solve your problem further you need to put .then function with callback over promise returned by employeeFactory.isSuperadministrator() like below

Controller

employeeFactory.isSuperadministrator().then(function(data){
   console.log(res);
});

Do follow same guidelines which are described here in this answer

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

5 Comments

Would still need .then in calling controller code.
@ste2425 could you look at referenced answer
That wasn't in your answer when i commented.
@PankajParkar Thanks again. But now I've to do the if statement in every controller again and again right?
@jamie that is how you should deal with promise(asynchronous) code.. Its regular pattern you should follow. Thanks :-)
0

Try

employeeFactory.isSuperadministrator().then(function(res){
console.log(res);
});

Comments

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.