0

I have some code like below:

$scope.offer = [];
angular.forEach($scope.details, function (product) {
  SomeAPI.ById.query({ product: product.id }, function (response) {
    $scope.offer.push(response);
  });
  return $scope.offer;
});
console.log($scope.offer);

Why do the console log still out put "[]"? On the page, {{ offer }} display value correctly.

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
James
  • 11
  • 4
  • Possible Duplicate of [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) – Tushar Feb 09 '16 at 03:25

1 Answers1

0

your API is async function

SomeAPI.ById.query({ product: product.id }, callback);  
console.log('query is still running. callback will be done later')

the callback above will be invoked later after asynchronous function completed. your console.log get called immediately

aifarfa
  • 3,939
  • 2
  • 23
  • 35