-1
angular.module('sample').factory('Service',
function( $rootScope, $http, $location )
{
    return {
        loadInfo: function()
        {
            var _url = 'url';
            $http.get( _url )
                .success( function( data )
                {


                    console.log(data);
                })
                .error( function( response )
                {
                    error( response );
                });
        },
});

I cannot call any other services without getting this result , but as it is asynchronous in weak networks it is getting time to load this results and end up with errors . How can i make this call to synchronous. Idea is other all services should wait until it loaded. In jquery ajax we can set a true or false very easily.But i am not able to do it in angular ,I refereed some other answers also but nothing seems to working .

Please suggest

Prashobh
  • 9,216
  • 15
  • 61
  • 91

2 Answers2

1

You can make use of angular promises. Documentation on $q service

angular.module('sample').factory('Service',
['$rootScope','$http','$location','$q',function( $rootScope, $http, $location,$q )
{
    return {
        loadInfo: function()
        {
            var _url = 'url';
            var deferred = $q.defer();
            $http.get( _url )
                .success( function( data )
                {

                    deferred.resolve(data);
                    console.log(data);
                })
                .error( function( response )
                {
                    deferred.reject(data);
                    error( response );
                });
        },
});

In your Controller it will look like this then:

Service.loadInfo.then(
     function(data){
         //Whatever you want to do with the data  on success     
     }
    ,function(data){
         //Whatever you want to do on error
     }
 );
V31
  • 7,626
  • 3
  • 26
  • 44
  • Thank you , but i am getting Cannot read property 'then' of undefined – Prashobh Jul 22 '14 at 08:10
  • I have corrected the syntax now it should not be defined @prash..Hope you injected the service though – V31 Jul 22 '14 at 08:16
  • no , i am still checking , i may missing some simple thing :( – Prashobh Jul 22 '14 at 10:48
  • It would be good if you can create a simple plunker of the code that you have so that we can also help you get rid of the problem, if desired. – V31 Jul 22 '14 at 11:07
1

Edit,

I have added $q service and some resolves and defers. Please check the updated answer.

It would have been really helpful if you could provide us with more code

You made a very simple error in your code

angular.module('sample').factory('Service',
function( $rootScope, $http, $location,$q )
{
    return {
        loadInfo: function()
        {
            var _url = 'url';
            var deferred = $q.defer();
            return $http.get( _url )
                .success( function( data )
                {

                    console.log(data);
                    deferred.resolve(data);
                })
                .error( function( response )
                {
                    error( response );
                    deferred.reject(response);
                });
        },
});

Edit In order to access 'then' , you need to return a promise, which I created using $q.defer() and returning deferred.resolve().

Cannot read property 'then' of undefined 
adib.mosharrof
  • 1,624
  • 1
  • 12
  • 16