I am currently exploring ways to enhance the performance of data loading by running requests in parallel or considering alternative methods. My background is in Java and I am relatively new to coding in Javascript/Typescript. I recently came across Async-Await and Promises which caught my interest.
Within my code, I have two methods - one that is independent and processes a single request, and another that relies on the output value of other requests (specifically the "number" value).
The sequence in which I call these functions is as follows:
private run(): void {
this.populatePostTextData(list, 3);
this.populateCustomerNameData(list, 5);
}
Here are my concerns:
Both methods do not appear to be executing in parallel simultaneously. The flow seems to wait for populatePostTextData()
to complete (and update the DOM), then proceed to populateCustomerNameData()
(updating the DOM again).
The second method involves two requests and I'm unsure how to structure the await
. I attempted to chain them together like so:
await Service.Current.executeRequest(getNumber).then((response: Response) => {
number = response.items[0]["CustomerNumber"];
console.log("Customer No retrieved: " + number);
await Service.Current.executeRequest(getNumber)...
}).catch((response: Response) => {
console.log("Error: " + response.errorMessage);
});
However, I encountered an error with
await Service.Current.executeRequest(getNumber)...
indicating it must be within an async function due to nesting. It doesn't seem to recognize it as part of an async method.
First Method
async populatePostTextData(list: IList, columnNum: number) {
const columnId = "C" + columnNum;
for (let i = 0; i < list.getData().getLength(); i++) {
let referenceOrderNo = list.getData().getItem(i).C1;
let referenceOrderLine = list.getData().getItem(i).C2;
const request = new Request("...");
let postTextData = {};
await Service.Current.executeRequest(request).then((response: Response) => {
let postText = response.items[0]["PostText"];
postTextData[columnId] = postText;
postTextData["id_" + columnId] = "R" + (i + 1) + columnId;
$.extend(list.getData().getItem(i), postTextData);
}).catch((response: Response) => {
console.log("Error: " + response.errorMessage);
});
}
}
Second Method
getNumber
should execute first as variable number
is utilized in getName
async populateCustomerNameData(list: IList, columnNum: number) {
const columnId = "C" + columnNum;
for (let i = 0; i < list.getData().getLength(); i++) {
let referenceOrderNo = list.getData().getItem(i).C1;
let referenceOrderLine = list.getData().getItem(i).C2;
let number = "";
const getNumber = new Request(".../");
await Service.Current.executeRequest(getNumber).then((response: Response) => {
number = response.items[0]["CustomerNumber"];
console.log("Customer No retrieved: " + number);
}).catch((response: Response) => {
console.log("Error: " + response.errorMessage);
});
let customerName = {};
const getName = new Request("...");
getName.setParam(1,number);
await Service.Current.executeRequest(getName).then((response: Response) => {
const name = response.items[0]["CustomerName"];
customerName[columnId] = name;
customerName["id_" + columnId] = "R" + (i + 1) + columnId;
$.extend(list.getData().getItem(i), customerName);
}).catch((response: Response) => {
console.log("Error: " + response.errorMessage);
});
}
}
I would greatly appreciate any suggestions or solutions.
Thank you.