I am trying to run a function 4 times (with different parameters) asynchronously with a Promise.all. This snippet runs, however it will only run each 'loopGridValidate' function in order synchronously. I have replaced the loopGridValidate function with the logic from this https://www.javascripttutorial.net/es6/javascript-promise-all/ tutorial and the Promise.all returns what is expected. I'm not sure what I am missing... The loop function is async, everything is set up correctly in the promise all (I have also split out the new promise's into 'const p1 = new Promise' and have the same effect as the code snippet below)
function validateCells() {
var grid = $("#NonStartUpGrid").data("kendoGrid");
var rows = grid.tbody.find("tr"); //rows from excel sheet
var valid = true;
var errorCount = 0;
var rowSec1 = Math.floor(rows.length * .25)
var rowSec2 = Math.floor(rows.length * .5)
var rowSec3 = Math.floor(rows.length * .75)
Promise.all([new Promise((resolve, reject) => { resolve(loopGridValidate(rows, grid, errorCount, 0, rowSec1, valid)); })
, new Promise((resolve, reject) => { resolve(loopGridValidate(rows, grid, errorCount, rowSec1 + 1, rowSec2, valid)); })
, new Promise((resolve, reject) => { resolve(loopGridValidate(rows, grid, errorCount, rowSec2 + 1, rowSec3, valid)); })
, new Promise((resolve, reject) => { resolve(loopGridValidate(rows, grid, errorCount, rowSec3 + 1, rows.length - 1, valid)); })
]).then(results => { const total = results.reduce((a, b) => a + b, 0) });
return errorCount;
};
async function loopGridValidate(rows, grid, errorCount, begin, end, valid) {
console.log(begin);
for (var i = begin; i <= end; i++) {
var rowModel = grid.dataItem(rows[i]);
if (rowModel) {
console.log(i);
var colCells = $(rows[i]).find("td[role=gridcell]");
for (var j = 0; j < colCells.length; j++) {
//custom logic
}
}
}
scrollToTop(grid)
if (valid) {
$("#SubmitGrid").prop("disabled", "");
}
$("#loading").prop("hidden", "hidden");
return errorCount;
}
Here is a fiddle to show a basic example of what I am talking about that is happening. If you click the button you will see in the console, the functions are ran synchronously, instead of asynchronously(the i values would be mixed up instead of in order) https://jsfiddle.net/tap16fbo/