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.

5
  • you want to call this service 1000 times ? may i know why Commented Jul 31, 2017 at 15:44
  • No, thats how many entries I will get each time Commented Jul 31, 2017 at 15:46
  • how many times you need to call the service can you please clarify the question Commented Jul 31, 2017 at 15:47
  • Depend on DB entries, if it have >1000 only 1, otherwise it will multiple times, for exemple... 50000 entries = 50 times Commented Jul 31, 2017 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 Commented Jul 31, 2017 at 16:37

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.