user1698144
user1698144

Reputation: 764

Angular - How to delay HTTP Requests

I have an application that loops through an array and makes an API call for each object in the array. The issue is all of the requests are being fired at once. The delay operator is completly ignored.

 public StartIt() {

    const len = this.stocks.length;
    console.log(len);

    for (const el of this.stocks) {
      this.iex.getFiveDay(el.Symbol).subscribe((res: any) => {
        console.log(res);
        this.ops.getVolAvg(res);
        this.datax.push(res);

      });



    }
  } 



 public getFiveDay(symbol) {
    const url: any = this.getApiUrl(symbol, iexEndPointType.fiveday);
    console.log('IEX SERVICE - getFiveDay(): ' + url);
    return this.httpClient.get(url).pipe(delay(5000));
  }

Upvotes: 1

Views: 1636

Answers (1)

Sameer
Sameer

Reputation: 5188

If you just want to add fixed delay of 5s for every request, then this might help you

const all = [1,2,3,4,5];
all.forEach((each, i) => {
    setTimeout(() => {
    console.log(each); // Call your http request
  }, i * 5000)
});

Upvotes: 2

Related Questions