【发布时间】:2019-12-04 18:30:51
【问题描述】:
在从我的商店 (ngrx) 检索到 2 个对象后,我需要执行一个操作,因此,在执行我的操作之前,我需要两个调用都做出响应,如下所示:
const item1$: Observable<Item> = this._store$.select(
ItemStoreSelectors.selectItemById(this.id1)
);
const item2$: Observable<Item> = this._store$.select(
ItemStoreSelectors.selectItemById(this.id2)
);
let item1: Item;
item1$.pipe(take(1)).subscribe((item: Item) => {
item1 = item;
});
let item2: Item;
item2$.pipe(take(1)).subscribe((item: Item) => {
item2 = item;
});
// here, items might not have been initialized
doSomething(item1, item2);
我尝试在 rxjs 中使用 switchMap、mergeMap 等寻找解决方案,但无法将其应用于我的需求。我想我找到了执行 async/await 的解决方案,但我不确定这是一个好习惯。
感谢您的帮助。
【问题讨论】:
-
使用 combineLatest learnrxjs.io/operators/combination/combinelatest.html
-
或视情况而定...
forkJoin. -
@MoxxiManagarm 会调查一下,谢谢,我想我已经尝试过了,但无法让它适用于我的案例
标签: angular async-await rxjs ngrx