0

I have been able to get this .factory to work by adding a "return" in front of both the Restangular.all statement and the response. My question is why is it needed there? Why can't I just return the response?

app.controller('MainController', ['GetIndexesFromES', '$scope', function(GetIndexesFromES, $scope) {
        $scope.indices = GetIndexesFromES.getUniqueIndexIDs();
        console.log($scope.indices);
}]);

app.factory('GetIndexesFromES', ['Restangular', function GetIndexesFromES (Restangular) {
    var GetIndexesFromES = {};
    GetIndexesFromES.getUniqueIndexIDs = function(){
        return Restangular.all('_stats/index,store').getList().then(function(response) {
            return response
        });
    }
    return GetIndexesFromES;
}]); 

The main reason why I'm asking this question is that I want to modify the data (within the .factory) before sending it back to the controller/$scope.

Thank you, Gregg

1 Answer 1

1

If you carefully look at your code for getUniqueIndexIDs you will realize there is a callback.

The second return is not a return from getUniqueIndexIDs but from your then's callback function.

Essentially your getUniqueIndexIDs returns a promise created by then. This promise is resolved by the return value then callback function which in your case is return response.

What you are essentially doing is promise chaining.

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

2 Comments

Hello, sorry for the delay. I had changed my code to this: ` getIndexesFromES.getUniqueIndexIDs = function(){ var localResponse = []; Restangular.all('_stats/index,store').getList().then(function(response) { angular.copy(listOfUniqueIndices(response), localResponse); }); return localResponse; }` In regards to your response it took me a while to understand all of what you were saying. The following link and sections helped: docs.angularjs.org/api/ng/service/$q section: The Promise API section: Chaining promises Thank you for your response/help.
Yes i realize that, need a better way to explain it.

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.