NGRX effects - mapping actions to make sequential service calls and updating store

落花浮王杯 提交于 2020-01-16 04:06:06

问题


I want to pass the same action 2 successive times with different entities, for example i want to add 2 entities,

entity 1;
entity 2;
this.store.dispatch(new Add(entity1));
this.store.dispatch(new Add(entity2));

the problem that i encountered is that i got the result of only one action (the entity 2 one) and this is the effect of my action. I want to wait for the result of the first action before passing the second action.

@effect()
add$ = this.actions$
.ofType<Add>(ADD)
.pipe(switchMap(a =>
this.sharedService.add(a.entity, a.api)
.pipe(map(s => new AddResult(Ok(s.id))))
.pipe(catchError(e => of(new AddResult(Err(e)))))));

回答1:


You may want to use mergeMap instead of switchMap. Here is list of Map with their behaviour:

@effect()
add$ = this.actions$
.ofType<Add>(ADD)
.pipe(mergeMap(a =>
this.sharedService.add(a.entity, a.api)
.pipe(map(s => new AddResult(Ok(s.id))))
.pipe(catchError(e => of(new AddResult(Err(e)))))));


来源:https://stackoverflow.com/questions/52758894/ngrx-effects-mapping-actions-to-make-sequential-service-calls-and-updating-sto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!