0
var suc=0;
            var failed=0;
            var tot=0;
            var body={};
            var f = (function() {
                for (var i=0; i< $scope.data.length; i++) {
                    (function() {
                        body = {
                            "data": {
                                email:$scope.data[i].mail,
                                firstName:$scope.data[i].name,
                            };
                        tot=tot+1;
                        if ($scope.group)
                            body.group = $scope.group;
                        $http({
                            url: API.toUrl('api/xxxxx/xxxxx'),
                            method: 'POST',
                            data: body
                        }).then(function(response) {
                            if (response.status == 200) {
                                suc = suc + 1;
                            }
                        },
                        function(error) {
                            if (error.status == 400) {
                                failed = failed + 1;
                            }
                        })
                    })(i);
                }
            })();
            console.log("Data: "+tot+" Success: "+suc+" Failed: "+failed);

The vars suc and failed when I show in console its 0 If I show the vars into the loop and in the IF statements works correctly.

Any idea? Thanks

ZizouJd
  • 79
  • 3
  • 12
  • at last, what output you are getting on console ? – Ravi Sep 04 '16 at 03:36
  • you have asynchronous code inside the loop, guaranteed that suc and failed will be 0 after the loop ends because NONE of the asynchronous code has even begun yet – Jaromanda X Sep 04 '16 at 03:36
  • Output its 3 - 0 - 0 – ZizouJd Sep 04 '16 at 03:37
  • 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) – Jaromanda X Sep 04 '16 at 03:38
  • You'll also probably have issues with `var body` definitely being "clobbered" by each iteration – Jaromanda X Sep 04 '16 at 03:44
  • this [fiddle](https://jsfiddle.net/xLnbf825/) possibly is what you want – Jaromanda X Sep 04 '16 at 03:52
  • @JaromandaX Error in fiddle `VM71:70 Uncaught (in promise) TypeError: (var)[Symbol.iterator] is not a function at Function.all (native) at window.onload` – ZizouJd Sep 04 '16 at 03:59

0 Answers0