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