2

I have a factory like this :

myapp.factory('JobOffersByCounteryFactory', function($http) {
    var factory = {
        getList: function() {
            return $http.get('data/offres_par_pays.JSON')
                .then(function(response) {
//....code

                }, function(error) {
                    return 'There was an error getting data';
                });
        },
        getHighestNumberOfOffers: function() {
            var n = 0;
            $http.get('data/offres_par_pays.JSON')
                .then(function(response) {
                    n = Math.max.apply(Math, response.data['nb']);
                }, function(error) {
                    return 'There was an error getting data';
                });
            return n;
        }
    };
    return factory;
});

This factory will return an object with two functions in it, the second function will return the maximum number in an array which is in this case response.data['nb']

In my controller I tested that function :

console.log(JobOffersByCounteryFactory.getHighestNumberOfOffers());

but I'm always getting the value 0.

I tried to affect some number to the n variable like this : n = 12; but I'm always getting the value 0 it seems that the value of the n variable didn't change inside $http.get('data/offres_par_pays.JSON').then(function(response)

How can I solve this ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • See [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) and [Why is my variable unaltered after I modify it inside of a function?](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). Short answer is that you can't return `n` based on `response`. Though, Angular already has one of the common suggestions – `$q` promises. – Jonathan Lonowski Dec 26 '15 at 19:35

1 Answers1

1

You can't expect asynchronous call to work synchronously. Because you could never know that when it will get completed.

For getting control over it you something which will tell that async call has been completed or failed, so that is what promise is. Basically $http.get has returns promise object. You should return a promise from the method getHighestNumberOfOffers, Place .then function over it which will have success and error callback in it. & on promise success you need to return the newly calculated variable value.

Code

getHighestNumberOfOffers: function() {
  var n = 0;
  return $http.get('data/offres_par_pays.JSON')
    .then(function(response) {
    n = Math.max.apply(Math, response.data['nb']);
    return n;
  }, function(error) {
    return 'There was an error getting data';
  });
}

While doing console.log(JobOffersByCounteryFactory.getHighestNumberOfOffers()) you will only get the promise object which is being returned by the getHighestNumberOfOffers(as per above suggestion). For getting exact value you should put that console.log inside the .then function of getHighestNumberOfOffers service method.

JobOffersByCounteryFactory.getHighestNumberOfOffers().then(function(n){
   console.log(n)
});
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299