0

I have a custom function that properly returns a cache object of user details. Additionally, the function checks to see if the cache is empty beforehand and makes an API call to retrieve the appropriate data if necessary. However, when I call this function from the angular ui-router resolve it still ends up getting undefined. I don't understand... because all my console printed statements within the custom function show that the correct object is being returned.

function getCache(UserService) {
    if(cacheIsEmpty) {
        UserService.getProfile().then(function (profile) {
            // check to see profile returned correctly
            console.dir(profile);

            // set cache using returned profile
            ...

            // create object loaded from cache
            var cacheObj = {
                firstName: // get from cache
                lastName:  // get from cache
                ...
            }

            // check if my object is correctly created
            console.log(cacheObj):

            // now return this correctly created object
            return cacheObj;

        });

    } else {

        // the following creates object directly from cache
        // since it is NOT empty and can be used
        var cacheObj = {
            firstName: // get from cache
            lastName:  // get from cache
            ...
        }

        // print to check object is correct
        console.dir(cacheObj);

        // now return it
        return cacheObj;
    }

}

And in the resolve, a call to this function returns as defined... despite the fact that the object is correctly being returned from the object.

...
resolve: {
    cacheObj: function(UserService) {

        // call the getCache function
        var returnedUserObject = getCache(UserService);

        // printing the object here shows that it is undefined
        console.log(returnedUserObject);

        // return the object to the controller
        return returnedUserObject;
    }
}
3
  • The return inside then() does not return to outer function. When cacheIsEmpty is true...nothing gets returned from getCache() Commented Dec 28, 2016 at 3:20
  • @charlietfl Oh wow! That makes sense. Thanks... but then... how do I handle this situation properly... because I need to ensure that the resolve waits for the object to return from the API call... otherwise just an empty object will be returned. Commented Dec 28, 2016 at 3:23
  • If a promise is returned in one case...a promise has to be returned in other case also. Just like @Pankaj is trying to show you Commented Dec 28, 2016 at 3:24

1 Answer 1

0

You should return a promise from getCache function, so that resolve recognise that till promise gets resolved I need to wait. Here you haven't returned a promise from if(cacheIsEmpty) method where you are making UserService.getProfile method call. Though returning data from promise from .then function doesn't mean you are returning promise.

Code

function getCache(UserService) {
    //return promise from both the places, if & else
    if(cacheIsEmpty) {
        //return promise here
        return UserService.getProfile().then(function (profile) {
            // check to see profile returned correctly
            console.dir(profile);

            // set cache using returned profile
            ...

            // create object loaded from cache
            var cacheObj = {
               firstName: // get from cache
               lastName:  // get from cache
               ...
            }
            //return cacheObj from here when promise resolved
            return cacheObj;
        });
    } else {
        //create cached cacheObj from cache and return it
        return $q.resolve(cacheObj)
    }
}

Resolve

resolve: {
    cacheObj: function(UserService) {
        return getCache(UserService);
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

First of all, thank you for the answer. The problem I am having is NOT that the resolve is not waiting for the profile API. Honestly, I have already implemented something similar to what you have suggested. The only reason I didn't provide all my code is because the problem is within the first if statement and how when the object is returned... it ends up being undefined within the resolve.
so basically the cacheObj that I am creating and returning is turning up as undefined even AFTER the resolve waits for its return.
@Grateful No way. May I know have you returned cacheObj from UserService.getProfile() .then function, Its needed there
Actually yes. It's my fault I didn't show this in the code above. Kindly let me update it to show more accurately what I mean. Thanks.
@Grateful No man, you haven't return UserService.getProfile() promise object.. please check my answer
|

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.