繁体   English   中英

Angular 4 / RxJS,按顺序发送数组的HTTP

[英]Angular 4 / RxJS, send HTTP of an array in sequence

尝试按顺序逐个发布数组的项。 知道应该使用RxJS flatMap但不能使其工作。 基本上我需要这样的东西:

item是名为items的数组的元素,要遍历数组并发送每个项目。

    this.http.post(url, item)
        .subscribe(
          (response) => {
            // call the same http.post again to send the next item
          },
          (error) => {
            this.app.error(error.json() as Error); // exit
          }         
        )

谢谢您的帮助。

也许尝试concat

Rx.Observable.concat(...items.map(item => this.http.post(url, item)).subscribe(res => doSmthWithResponse());
  this.http.post(url, item).map(res => res.json())
            switchMap(this.http.post(url1, item).map(res => res.json()))    

您可以尝试使用from和flatmap

Observable.from(items)
    .flatMap(item => this.http.post(url, item))
    .subscribe({
        res => console.log("your response"),
        error => console.log("your error"),
        () => doSomethingAtEnd()
    });

暂无
暂无

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

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