Angular 主题不起作用的concatMap,http.post起作用
我有一个要按顺序发布到服务器的项目数组:Angular 主题不起作用的concatMap,http.post起作用,angular,rxjs,Angular,Rxjs,我有一个要按顺序发布到服务器的项目数组: const items = ["a", "b", "c"]; 为了实现这一点,我将concatMap与Angular的HttpService结合使用。但是,我不想直接在concatMap中使用HttpService调用,而是使用第三个主题: sendItem(item: Item): Subject<boolean> { const result$ = new Subject(); console.log(`Sending
const items = ["a", "b", "c"];
为了实现这一点,我将concatMap与Angular的HttpService结合使用。但是,我不想直接在concatMap中使用HttpService调用,而是使用第三个主题:
sendItem(item: Item): Subject<boolean> {
const result$ = new Subject();
console.log(`Sending ${item}`);
this.http.post('http://...', item)
.subscribe(() => {
// do stuff
console.log(`Done sending ${item}`);
result$.next()
});
return result$;
}
sendItems() {
Observable.from(items)
.concatMap(item => this.sendItem(item))
.subscribe(() => console.log('Done with all items'););
}
我做错了什么?好的,我找到了解决方案: concatMap不会订阅下一个可观察对象,直到上一个完成 所以,如果我做一个$complete结果,一切都很好
sendItems() {
Observable.from(items)
.concatMap(item => this.http.post('http://...', item))
.subscribe(() => console.log("Done with all items"));
}