Mulder3
Mulder3

Reputation: 55

Rxjs: Execute a list of async operation one after the other and wait until finish

There is a list of request (url strings) that are run one after the other and wait until the last is finish. The reason is, every request is expensive for the backend.

Is there a possible to programming in RxJs?

return Observable.from([list of urls])
  // ???
  .switchMap((url: string) => {
     return this.http.get(url)
  }).
  // wait until last is finish

Using:

Thank you

Upvotes: 0

Views: 286

Answers (1)

Picci
Picci

Reputation: 17762

Probably what you are looking for is something like this

return Observable.from([list of urls])
  .mergeMap((url: string) => {
     return this.http.get(url)
  }, 1)
.subscribe(result => // do something with the result)

Consider the second parameter of mergeMap. That parameter sets the level of parallelism of the operator. Setting it to 1 means that there will be only 1 request processed at the time.

If you feel that the server can serve more requests in parallel, you can change that parameter.

Upvotes: 1

Related Questions