0

Is there a way to call a service multiple times? I want to load some data from my DB and in case its a long list, I want to load 1000 entries at each request, if I do something like:

while (!done) { ... }

I will get to many requests to DB that aren't needed but I could get all my data.

This is my method on 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 this is 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);

}

Any advice how I can do it? Thank you in advance for the help.

Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
João Silva
  • 531
  • 4
  • 21
  • 40
  • you want to call this service 1000 times ? may i know why – Rahul Singh Jul 31 '17 at 15:44
  • No, thats how many entries I will get each time – João Silva Jul 31 '17 at 15:46
  • how many times you need to call the service can you please clarify the question – Rahul Singh Jul 31 '17 at 15:47
  • Depend on DB entries, if it have >1000 only 1, otherwise it will multiple times, for exemple... 50000 entries = 50 times – João Silva Jul 31 '17 at 16:26
  • You can make use of shared services have this method in service and expose a variable in service that is updated by this call. When the variable changes it will notify the observing component and update the value and then you can check . What to do call that service again or not.in a if else – Rahul Singh Jul 31 '17 at 16:37

1 Answers1

2

If you want to perform n repetitions of a Promise returning operation sequentially one after another there are a few ways.

The most syntactically pleasant and readable way would be to write an async function with a loop and use await in the loop like so.

async function f(n) {
  for (let i = 0; i < n; i += 1) {
    const response = await fetch('api/items?page=' + i);
  }
}

We can do this without the async/await sugar but this is a poster child use case for it.

Without the syntactic sugar, we could write

function f(n) {
  for (let i = 0, p = Promise.resolve(); i < n; i += 1) {
    p = p.then(() => fetch('api/items?page=' + i));
  }
  return p;
}
Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84