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.