Currently facing an issue with my code where the updateProduct() function call is happening before the forEach loop begins running on about 1 out of every 10 page loads. Not sure why this is occurring or how to solve it. Any insights on what might be causing this and how to prevent it from happening again?
I attempted using a promise, which successfully resolved the issue on around 7 out of 10 page loads. Currently, it is successful about 9 out of 10 times. It's puzzling how it manages to complete a loop before even starting.
$http.get('file.json').success(function(data) {
$scope.products = data;
$scope.product_map = {};
var itemsProcessed = 0;
$scope.products.forEach(function(item) {
$scope.product_map[item.sku] = item;
itemsProcessed++;
if(itemsProcessed === $scope.products.length) {
$scope.seriesId = $scope.series_map[$scope.hashTag].seriesID;
updateProduct($scope.series_map[$scope.hashTag].products[0].sku, $scope.series_map[$scope.hashTag].products[0].image);
}
});
});