1

My code is:

return this.creaClienti(cliente)
      .pipe(
        tap(res => console.log('Cliente ->', res)),
        concatMap(res => this.creaIntolleranza(intolleranza)),
        tap(res => console.log('Intolleranza ->', res)),
        concatMap(res => this.creaSpaziUtilizzati(utilizzoSpazi)),
        tap(res => console.log('Utilizzo spazi ->', res)),
        concatMap(res => this.creaEvento(evento))
      );
  }

but this.creaClienti(cliente) is:

 creaClienti(clienti: any[]): Observable<any> {
    return from(clienti).pipe(
      concatMap(cliente => <Observable<any>>this.http.post(environment.baseUrl + 'api/json/node/cliente', cliente, this.httpOptions))
    );
  }

the problem is that every time a contained call is ended the pipe restarts...

I need to run multiple call lists sequentially, all the functions that are in the concatMap are in fact similar to creaClienti

3
  • That sounds correct. You can append shareReplay() and multiple subscriptions shouldn't trigger the source Observable. Commented Jan 9, 2020 at 11:51
  • I do not want that for every call made in creaClienti it starts the pipe again. Commented Jan 9, 2020 at 11:55
  • What exactly do you mean with the pipe starts again? Where are you subscribing to those observables? .subscribe or aync pipe? Commented Jan 9, 2020 at 13:29

1 Answer 1

1

I guess you want all your functions (this.creaClienti, this.creaIntolleranza, this.creaSpaziUtilizzati, this.creaEvento(evento)) to only emit once when all inner http calls completed.

If e.g. creaClienti should only emit once all internal calls are done you can add last or toArray depending on the output you want.

creaClienti(clienti: any[]): Observable<any> {
  return from(clienti).pipe(
    concatMap(cliente => <Observable<any>>this.http.post(environment.baseUrl + 'api/json/node/cliente', cliente, this.httpOptions)),
    last() // only emit the last http response
    // or toArray() // emit all http response in an array when the last one completed
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.