2

I have a REST call in service layer on which I have defined a promise which is making this asynchronous call a synchronous one and I am calling it from my controller method. Below is the code:

service method:

app.lazyload.factory('myService',['$http','$q', function($http,$q) {

        return{
            showAll :function ()
            {
                var deferred = $q.defer();
                $http.post('rest/getAll?cd='+ (new Date()).getTime())
                .success(function(data)
                {
                    deferred.resolve(data);
                })
                .error(function(data)
                {
                    deferred.reject(null);
                    console.log("in error block");
                });

                 return deferred.promise;
            }
        };

    }]);

controller method:

$scope.showAll = function()
                {
                    var promise = myService.showAll();

                    promise.then(function success(data) {
                        $scope.allitems = data;
                        console.log(data);
                        console.log('$scope.allitems'+$scope.allitems[0].name);
                        $scope.showAllitems = true;
                        blockMyUI();
                    }, function error(msg) {
                      console.error(msg);
                    });

                };

While debugging this javascript if I halt it for 2 sec i get the response but i don't get it if done non-stop. This means all REST call are working fine but there is some problem in my 'promise'. This promise is not waiting for REST call to complete which I want.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
rishi
  • 1,792
  • 5
  • 31
  • 63
  • Do you want `var promise = myService.showAll();` to be synchronous? – Aniket Sinha Dec 17 '14 at 11:59
  • Related: [What is the deferred anti pattern and how do I avoid it?](http://stackoverflow.com/questions/23803743/what-is-the-deferred-antipattern-and-how-do-i-avoid-it) – Benjamin Gruenbaum Dec 17 '14 at 12:30
  • 1
    Your code looks fine, please create a short self contained example illustrating the problem on jsfiddle or plunkr or a similar service or using stack snippets. – Benjamin Gruenbaum Dec 17 '14 at 12:33
  • @AniketSinha I want this REST call to be synchronous. – rishi Dec 17 '14 at 12:50
  • 1
    @rishi Are you saying that the values are not populating in your app, or are you saying that `myService.showAll()` is not sitting and waiting until the data comes back? You shouldn't be trying to make an HTTP request synchronously in JavaScript. The point of promises is to make asynchrony more manageable, not to make it synchronous. – JLRishe Dec 17 '14 at 12:57
  • As far as I know, whatever you write in angular u will get asynchronous code with $http. – Petr Averyanov Dec 17 '14 at 13:13
  • Your promise _is_ waiting for your REST call, your problem does not reproduce in a clear example. – Benjamin Gruenbaum Dec 17 '14 at 14:48

1 Answers1

0

Try using $watch in controller.

var dataFromFactory = '';
myService.showAll().then( function(data){
   dataFromFactory = data;
}
$scope.$watch(function () {
    return dataFromFactory;
},
function (val) {
        $scope.allitems = val;
        console.log(val);
        //the rest value you want.
}, false);
syarul
  • 2,161
  • 2
  • 20
  • 22