Is there a way to optimize the number of requests made to a service? I need to retrieve data from my database in batches of 1000 entries each time. Currently, I have a loop set up like this:
while (!done) { ... }
This approach results in unnecessary requests being sent to the database. Is there a better way to efficiently fetch all the required data?
Here is the method defined in my component.ts:
lazyLoadData(): void {
this.getUntil += 1000;
if (this.getUntil > this.totalEntries) {
this.getUntil = this.totalEntries;
}
this.salesService.GetEDISalesReportDetailListLazy(this.id, this.getFrom, this.getUntil)
.then(data => {
if(this.dataTable == null) {
this.dataTable = [];
this.dataTable = data;
this.isDataTableAvailable = true;
this.status = this.dataTable.aaData[0].Status;
} else {
for(let i = 0; i< data.aaData.length; i++){
this.dataTable.aaData.push(data.aaData[i]);
}
this.getFrom = this.getUntil;
}
});
}
And here is the implementation in my service.ts:
GetEDISalesReportDetailListLazy(id: number, startFrom: number, until: number): any {
return this.http2.post(SERVICE_URL + 'Sales/GetEDISalesReportDetailListLazy', { id: id, startFrom: startFrom, until: until }, this.options)
.toPromise()
.then(response => {
return response.json().ReturnStatus.ReturnObject;
})
.catch(this.handleError);
}
If you have any suggestions on how to improve this process, I would greatly appreciate it. Thank you for your assistance.