0

I have a global array defined outside of all functions like so:

var invoice2016Header = new Array(12);

I then populate it in my loadArray() function:

function loadArray() {

    var promiseArray = [];

for (i = 0; i < 12; i++) {

        promiseArray.push(
            new Promise(function (resolve, reject) {
                runOWSLS("Invoice", beginning2016Months[i], closing2016Months[i], "no", function (callbackResp) {
                    invoice2016Header[i] = callbackResp.header.ynofreight;
                    alert(invoice2016Header[i]); //This returns the CORRECT value
                    resolve();
                });
            })
        );

    }


    Promise.all(promiseArray).then(function () {

            for (i = 0; i < 12; i++){

                alert(invoice2016Header[i]); //This returns UNDEFINED for every value??
            }


        });
}

Since the invoice2016Header[] array is in the global scope I fail to see why when it's referred to when the promise calls it I get UNDEFINED. Shouldn't it have the value that assigned to it earlier?

The way JS does scopes throws me off sometimes..

--EDIT--

Here is the last bit of the console log:

monthlyCharting.js:148 OWSLS Ran Successfully
monthlyCharting.js:149 Object {data: Object, status: 200, config: Object, statusText: "OK"}
monthlyCharting.js:437 422351.60
monthlyCharting.js:148 OWSLS Ran Successfully
monthlyCharting.js:149 Object {data: Object, status: 200, config: Object, statusText: "OK"}
monthlyCharting.js:437 242180.36
monthlyCharting.js:452 12 - undefined
  • 2
    Something tells me that the code you're actually seeing this problem in is different than exactly what you posted here. – jfriend00 Jul 25 '16 at 21:18
  • 1
    One possible problem is that you're using an implicitly global `i`. Please declare that variable with `var` or `let` in each of the two places you use it. – jfriend00 Jul 25 '16 at 21:22
  • Are you doing this in Angular? It doesn't look like it but this question is tagged. – austinthedeveloper Jul 25 '16 at 21:22
  • Also, remove all `alert()` statements and use `console.log()` instead. `alert()` hides asynchronous problems because it affects the timing of running the code. – jfriend00 Jul 25 '16 at 21:23
  • Use `debugger;` to actually stop JS processing and see if the variables have your expected values. – Taylor Daughtry Jul 25 '16 at 21:24
  • Yes, this is in AngularJS. I removed the alerts and replaced them with console.log, but with the same issue. The console logs the correct values, but then logs 12 undefineds. – Kevin Souter Jul 25 '16 at 21:54
  • Here is the last bit of the console log: `monthlyCharting.js:148 OWSLS Ran Successfully monthlyCharting.js:149Object {data: Object, status: 200, config: Object, statusText: "OK"} monthlyCharting.js:437 422351.60 monthlyCharting.js:148 OWSLS Ran Successfully monthlyCharting.js:149 Object {data: Object, status: 200, config: Object, statusText: "OK"} monthlyCharting.js:437 242180.36 monthlyCharting.js:452 12 - monthlyCharting.js:452 undefined` – Kevin Souter Jul 25 '16 at 21:59

1 Answers1

0

I changed the way in which the promise is called to the code below, and it now works:

function loadArray() {


    ...


var promiseArray = [];


var get2016Totals = function (i) {
        return new Promise(function (resolve) {
            runOWSLS("Invoice", beginning2016Months[i], closing2016Months[i], "no", function (callbackResp) {
                $scope.invoice2016Header[i] = callbackResp.header.ynofreight;
                console.log($scope.invoice2016Header[i]);
                resolve();
        });
    })
}

for (var i = 0; i < 12; i++) {

    promiseArray.push(get2016Totals(i));

}


Promise.all(promiseArray).then(function () {

        for (var month = 0; month < 12; month++){

            console.log($scope.invoice2016Header[month]);
        }

        google.charts.load('current', {packages: ['corechart', 'bar']});
        google.charts.setOnLoadCallback(drawChart);
    });
}