"AddDistance" is an insert to DB function, the problem is that hash[k][2] value jumps forward with the loop while "distance"(the service promise) is still waiting, and as result i insert wrong row to my DB.
how do i hold the loop from jumping to the next object before "AddDistance()" ran, should i use Q$, While, .then()?
i tried the above but failed, probably didn't do it correctly.
Please advise...
for (var k in hash)
{
GoogleMaps(hash[k][0], hash[k][1], function (x) {
var distance = x.rows[0].elements[0].distance.value;
distance = parseInt(distance / 1000);
AddDistance({ CityID1: hash[k][2], CityID2: $scope.CompanyCity.Id, DistanceKM: distance });
console.log(distance);
//return distance;
});
}