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)
});