0

I have a little problem on my app. I would like to get and return the data array outside the anonymous function. I use promise, aim my problem is when I try my service it return a random array lenght with random values.

I do not know the problem, I do not know if I use the promise.

getCurrentExchangeTo : function(year, month, country){

            var def = $q.defer();

            var numberDayPerMonth = [
                31,
                9,
                31,
                30,
                31,
                30,
                31,
                30,
                31,
                30,
                31,
                30,
            ];

            var vm = this;
            this.country = country;

            this.getCurrentExchangeFor = [];
            var hello = "gello"

            for(var i = 0; i < numberDayPerMonth.length; i++){
                if((i + 1) === month){
                    for(let j = 1; j < numberDayPerMonth[i]; j++){
                        $http.get('http://api.fixer.io/2000-02-0' + j + '?symbols=USD').then(function (success) {
                            let countryDay = vm.country                                
                            vm.getCurrentExchangeFor[j] = success.data.rates[countryDay];
                            def.resolve(getCurrentExchangeFor)
                        });
                    }
                }
            }
            return def.promise
        }

and

    getCurrentExchangeService.getCurrentExchangeTo(2015, 2, 'USD').then(function (data) {
        console.log(data)
    });
4
  • javascript is async.. when u call the api the loop continues and keeps calling the api. Each call returns in different time.. so you get random numbers... Commented May 31, 2017 at 20:09
  • This right .. But do you have an idea for result my problem ?? Commented May 31, 2017 at 20:49
  • Your return statement should come after the resolve statement in your loop. Commented May 31, 2017 at 21:11
  • @MikeFeltman I return my variable after my loop, I don't understand.. Commented May 31, 2017 at 21:34

1 Answer 1

3

You are overcomplicating things.

In particular, the outer loop isn't necessary, neither is the Deferred.

$http.get() clearly retuns a promise, which can be pushed onto an array and finally aggregated with $q.all().

As far as I can tell, you want the following :

getCurrentExchangeTo: function(year, month, country) {
    var numberDayPerMonth = [ 31, 29, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 ];
    var promises = [];
    for(let i=1; i<=numberDayPerMonth[month-1]; i++) {
        promises.push($http.get('http://api.fixer.io/2000-02-0' + i + '?symbols=USD').then(function(response) {
            return response.data.rates[country];
        }));
    }
    return $q.all(promises);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You need to check that numberDayPerMonth array - April, June, September & November have 31 days, Also, for February, with reference to this answer, you can write [..., leapYear(year)?29:28, ...].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.