繁体   English   中英

Angular 8 RXJS - 按顺序进行多个 HTTP 调用

[英]Angular 8 RXJS - Make multiple HTTP calls sequentially

我的代码是:

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

我猜你希望你的所有函数( this.creaClientithis.creaIntolleranzathis.creaSpaziUtilizzatithis.creaEvento(evento) )在所有内部 http 调用完成时只发出一次。

如果例如creaClienti应该只在所有内部调用完成后发出,您可以根据您想要的输出添加lasttoArray

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM