简体   繁体   中英

Asynchronous calls in Angular

Probably a naive question but I wanted to understand, and that's why posting the question.

I have to make three different API calls and store some values in an array. I am using this to achieve that:

getNodeDetails() {
 this.httpClient
        .get<string[]>(this.nodeURL)
        .subscribe(res => {
                                this.nodeCount = Object.keys(res).length;
                       Object.values(res).forEach(value => {
                             this.nodeArray.push(value);
                         });
                       this.getTotalTransactions();
                });
}

getTotalTransactions(): void {
      this.httpClient
        .get(this.transactionURL)
        .subscribe((data) => {
                     this.numberOfTransactions = data.length;
                     data.forEach(apiData => {
                    this.totalUniqueTransactionId.push(apiData.uniqueTransactionId);
                    this.dateArray.push(apiData.requestTime);
                });
                     this.totalUniqueTransactionId.forEach(transactionId =>
                                this.column1.push(transactionId.substr(0, transactionId.indexOf('-'))));
        });

      let newData: any = [];

         this.nodeArray.forEach(node => {
                   this.httpClient.get(this.URL1 + node).subscribe(data => {
                    console.log(data);
                    this.dynamicColumns.push(data);
                });
         });
         this.getDataMapping();
    }

Now the problem that I am facing is that these calls need to be in such a way that I need all the data before I render the table. I am getting the response, but I want to make it in such a way that I need to obtain the results for the API before calling the getDataMapping() function.

Can anyone advise me for the same?

EDIT : since the API is called in the loop, arrays needed to be received before processing the furthur logic. This is what I couldn't find while searching over.

Thanks

you can define three booleans:

  1. gotNodeDetails = false
  2. gotTotalTransactions = false
  3. gotDataMapping = false

after 1 and 2 booleans are true, you can call:

this.getDataMapping();

and you can use a ngif for rendering your table with boolean 3.

<div *ngIf="gotDataMapping">Content to render when condition is true.</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM