I'm not entirely sure what you're doing, but rewriting this to an
async
function is fairly simple:
$(async function() {
const numberOfReports = @Model.Count();
const reports = @Html.Raw(Json.Encode(Model.Select(x => x.ReportName)));
const delay = (time, value) => new Promise(resolve => setTimeout(resolve, time, value));
let divId = 1;
let arrayId = 0;
let processedReports = 0;
for (let i = 0; i < numberOfReports; i++) {
if (numberOfReports === processedReports) { break; }
if (numberOfReports >= 3) {
const reportLinks = reports.slice(arrayId, arrayId + 3);
const postData = { threeCells: reportLinks };
const result = await $.ajax({
type: "POST",
url: '@Url.Action("ThreeCells", "Home")',
data: postData,
dataType: "html"
});
const threeDiv = document.createElement('div');
threeDiv.setAttribute('id', divId);
document.querySelector('#inner').appendChild(threeDiv);
$('#' + divId).html(result);
divId++;
arrayId = arrayId + 3;
processedReports = processedReports + 3;
}
else if (numberOfReports === 2) {
const reportLinks = reports.slice(arrayId, arrayId + 2;
const postData = { twoCells: reportLinks };
const result = await $.ajax({
type: "POST",
url: '@Url.Action("TwoCells", "Home")',
data: postData,
dataType: "html"
});
const twoDiv = document.createElement('div');
twoDiv.setAttribute('id', divId);
document.querySelector('#inner').appendChild(twoDiv);
$('#' + divId).html(result);
divId++;
arrayId = arrayId + 2;
processedReports = processedReports + 2;
}
else {
break;
}
await delay(1000);
}
});