Angular 4 / RxJS,顺序发送一个数组的HTTP
Angular 4 / RxJS, send HTTP of an array in sequence
正在尝试依次 http post 数组的项目。知道应该使用 RxJS flatMap 但无法使其工作。基本上我需要这样的东西:
item是一个名为items的数组的元素,要遍历数组并发送每个item。
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
}
)
感谢您的帮助。
this.http.post(url, item).map(res => res.json())
switchMap(this.http.post(url1, item).map(res => res.json()))
也许试试concat
Rx.Observable.concat(...items.map(item => this.http.post(url, item)).subscribe(res => doSmthWithResponse());
你可以尝试使用 from 和 flatmap
Observable.from(items)
.flatMap(item => this.http.post(url, item))
.subscribe({
res => console.log("your response"),
error => console.log("your error"),
() => doSomethingAtEnd()
});
正在尝试依次 http post 数组的项目。知道应该使用 RxJS flatMap 但无法使其工作。基本上我需要这样的东西:
item是一个名为items的数组的元素,要遍历数组并发送每个item。
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
}
)
感谢您的帮助。
this.http.post(url, item).map(res => res.json())
switchMap(this.http.post(url1, item).map(res => res.json()))
也许试试concat
Rx.Observable.concat(...items.map(item => this.http.post(url, item)).subscribe(res => doSmthWithResponse());
你可以尝试使用 from 和 flatmap
Observable.from(items)
.flatMap(item => this.http.post(url, item))
.subscribe({
res => console.log("your response"),
error => console.log("your error"),
() => doSomethingAtEnd()
});