[Fixed] make synchronous HTTP calls in Angular 11


I created a service to get product details from product id like the following:

public async getXeViaId(id_xe:string)
  const path =`${this.apiServerURL}/xe/get_xe_via_id?id_product=${id_xe}`;
  const result = this.http.get(path).toPromise();
  return result;

Then I test like the following


Then it work fine but when I try with for loop like the following:

for(let i =0;i<3;++i)
      let product:any

When I debug I realize that the cart push value null,its meaning value of product that I received is null.

How I can wait until HTTP request of one for loop is finished and then push it to cart. I tried to solve it using Promise and many way that use Promise to solve that problem but none of them worked fine.
Can you guys fix it help me.It take a lot of my time


const cart = [];
for(let i =0;i<3;++i) {
    await this.xeService.getXeViaId("XE1700000082")

Leave a Reply

(*) Required, Your email will not be published