0

I have a function inside a angular js controller. I try to do a while cycle but the stop condition is a result of a promise that I execute in a service. The result is that the cycle is infinity.

My code:

$scope.origin = "table_name";
$scope.start = 1;
$scope.end = new Date().getTime();

$scope.setData = function () {
    var flag = true;
    while(flag)
    {
        service.getData($scope.origin, $scope.start, $scope.end, 0, 100)
                .then(
                        function(res)
                        {
                            if(res.length == 100)
                            {
                                $scope.start = res[res.length - 1].timestamp;
                            }
                            else
                            {
                                flag = false;
                            }
                        },
                        function(err)
                        {
                            flag = false;
                        }
                );
    }
}
maikelm
  • 403
  • 6
  • 30
  • Javascript is single threaded. The while loop runs infinitely and doesn't give the engine a chance to return the promise to exit the loop. – Will Feb 26 '17 at 19:10
  • A loop is executed synchronously, it won't wait for the promise. Use recursion. – Bergi Feb 26 '17 at 19:39

0 Answers0