Angular 8 RX JS - Выполнение нескольких HTTP-вызовов последовательно - PullRequest
1 голос
/ 09 января 2020

Мой код:

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))
      );
  }

, но this.creaClienti (cliente)::

 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))
    );
  }

проблема в том, что каждый раз, когда отдельный вызов завершается, канал перезапускается. .

Мне нужно последовательно запустить несколько списков вызовов, все функции в concatMap на самом деле похожи на creaClienti

1 Ответ

0 голосов
/ 09 января 2020

Полагаю, вы хотите, чтобы все ваши функции (this.creaClienti, this.creaIntolleranza, this.creaSpaziUtilizzati, this.creaEvento(evento)) излучали только один раз, когда все внутренние вызовы http завершены.

Если, например, creaClienti следует Издайте только после выполнения всех внутренних вызовов, вы можете добавить last или toArray в зависимости от желаемого выхода.

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
  );
}
...