RxJS make multiple AJAX requests then get a new list with another AJAX request

237 Views Asked by At

The goal is to make multiple save request and then get a new list using another ajax request. I have the following that appears to be what I want but was wondering if there was a better way

of(1,2,3)
      .pipe(
        concatMap(vals => vals),
        concatMap(val => this.ajax.save(val)),
        takeLast(1)
      )
      .pipe(
        switchMap(() => this.ajax.get()),
        take(1)
      )
      .subscribe(result => {
        // do stuff
      });

The results of the saves are not important. Getting the new results after the saves is the result that I need to use. I used takeLast(1) because if I don't there will be a get request for every item in the list. In the example above I would have 3 get request and I only want to get the list after all the saves are done.

1

There are 1 best solutions below

0
On BEST ANSWER

What about

combineLatest([1,2,3].map(val => this.ajax.save(val))).pipe(
  switchMap(resultsArr => this.ajax.get())
).subscribe(newVal => {});