Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

rxjs sequential dynamic number of api calls

Trying to create an rxjs pipeline that will do the following: Essentially I have an array of a dynamic number of API calls. Every time I need to make a new API call, I push it to the array.

Then, I transform the array into an array of RXJS switchmap operators. (The sequence of the calls doesn't matter, just that I need to make one call after the previous call finishes, and not at the same time).

Finally, I try to spread the array of rxjs operators inside a pipe:

const obsArray = [randomQuote(), getCatFacts(4), recipeSearch('oil')]
const OperatorWrap = obsArray.map((obs) => {
  return rxops.switchMap((val) => { return obs })
})



const pipeline= of(1).pipe(...OperatorWrap);

But I get a strange error message: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

concatAll it is what do you need

concatAll - Collect observables and subscribe to next when previous completes.

I did an example: https://stackblitz.com/edit/angular7-rxjs-jy2gda?file=src/app/app.component.ts

    const APIs = [
      `https://jsonplaceholder.typicode.com/todos/1`,
      `https://jsonplaceholder.typicode.com/todos/2`,
      `https://jsonplaceholder.typicode.com/todos/3`,
      `https://jsonplaceholder.typicode.com/todos/4`
    ].map(url => this.http.get(url));
    from(APIs)
      .pipe(concatAll())
      .subscribe();

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...