0

I need to make 100s of http requests to a gated API, 5 requests per second. Using Angular 6 with CLI and RXjs. On the Node.js server, I know how to do this with request-retry NPM package. How do I do it with Angular 6?

I know how to make a single request, or how to many many requests, but how do I insert a time delay so that only 5 requests are made a second?

getApps(luisAuthoringKey:string): Observable<any> {

  this.httpOptions.headers.set("Ocp-Apim-Subscription-Key",luisAuthoringKey);

  return this.http.get(this.endpoint + 'apps', this.httpOptions).pipe(
    map(this.extractData));
}
martin
  • 93,354
  • 25
  • 191
  • 226
DFBerry
  • 1,818
  • 1
  • 19
  • 37
  • how to do a lot of request ? – Abel Valdez Sep 09 '18 at 02:18
  • 1
    Possible duplicate of [Node.js - Working with an API limit of 5 requests per second](https://stackoverflow.com/questions/46701209/node-js-working-with-an-api-limit-of-5-requests-per-second) – Rafael Sep 09 '18 at 02:39
  • I know how to do it in Node.js --- I don't know how to do it in Angular 6/Typescript. Angular's HTTP library is different than Node.js. They are not interchangeable. – DFBerry Sep 09 '18 at 02:44

2 Answers2

2

use interval or timer operators from rxjs

With timer

this.http.get(this.endpoint + 'apps', this.httpOptions).pipe(
  map(this.extractData),
  timer(200),
  catchError(e=>of(e)),
  repeat(100),
)

With Interval

interval(200).pipe(
  this.http.get(this.endpoint + 'apps', this.httpOptions),
  map(this.extractData),
  catchError(e=>of(e)),
  take(100),
)
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39
0

I think the best option here is bufferCount operator and then delay to ensure the minimum 1s delay.

from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
  .pipe(
    concatMap(v => of(v).pipe(delay(120))),
    bufferCount(5),
    concatMap(buffer => of(buffer).pipe(delay(1000)))
  )
  .subscribe(console.log);

Live demo: https://stackblitz.com/edit/rxjs6-demo-k5wins?file=index.ts

martin
  • 93,354
  • 25
  • 191
  • 226