am i making synchronous call for this service and using that service properly in controller. please help me : > my brother said to write synchronous call for service and below
> getting error. can u please explain and give me the right one, thanks
> you.
below i gave my angular service and Controller

> my brother said to write synchronous call for service and below
> getting error. can u please explain and give me the right one, thanks
> you.
below i gave my angular service and Controller



<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    app.service("StateService", function ($http, $q) {
        var factory = $q.defer();
        factory.getStateData = function () {
            return $http.get("/State/GetStateData")
                .then(function (response) {
                    factory.resolve(response.data);
                    return factory.promise;
                }, function (response) {
                    factory.reject(response);
                    return factory.promise;
                }); 
        };
          factory.getCountryById = function (Id) {
            return $http.post('/Country/GetCountryById?Id=' + Id)
                .then(function (response) {
                    factory.resolve(response.data);
                    return factory.promise;
                }, function (response) {
                    factory.reject(response);
                    return factory.promise;
                });
        };
        };
        
        
    app.controller("statecontroller", function ($scope, StateService, $http, $timeout) {    
        // get function
        $scope.GetStateData = function () {
            StateService.getStateData().then(function (d) {           
                $scope.States = d.data;
                $scope.fillAllData();
            }, function (response) {
                alert('error occurred' + response.data.ExceptionMesage);
            });
        };
        
        
        $scope.fillAllData = function () {
            for (var i = 0; i < $scope.States.length; i++) {
                if ($scope.States[i].CountryId)
                {
                    // var val = $scope.GetCountryById($scope.States[i].CountryId);
                    var cName = "";
                    var Id = $scope.States[i].CountryId;
                    StateService.getCountryById(Id).then(function (d) {
                        if (d.data)
                            cName = d.data.CountryName;
                    }, function (response) {
                        console.log('error occurred' + response.data.ExceptionMesage);
                    });               
                    $scope.States[i].CountryName = cName;
                }               
                else
                    $scope.States[i].CountryName = null;
            }
        }

    };

<!-- end snippet -->

