angular - 一个 @ngrx/effect 中的多个 Http 调用
问题描述
我很困惑,并且是@ngrx 的新手,所以请原谅我不理解这应该如何工作。
假设我有一个叫做的效果PlaceOrderEffect
在这种情况下,我想按顺序处理每个请求。
processOrder$ = createEffect(
ofType(OrderActions.PROCESS_ORDER),
//here is where i get confused
concatMap([
this.service.addCrust(),
this.service.addTopings(),
this.service.sendToCook()
this.service.checkOut()
]
)
如何按顺序运行这些并处理最终响应?
解决方案
concatMap
isRxJS
运算符与 一起使用pipe
,将源 observable 与返回的新 observable 合并concatMap
,这不是你的情况。
如果请求不相互依赖,我认为你可以使用RxJS
concat
函数来实现你想要做的事情,如下所示:
processOrder$ = createEffect(() =>
this.actions$.pipe(
ofType(OrderActions.PROCESS_ORDER),
// here you need to switch and map to the new observable (concat one) returned by switchMapTo operator
switchMap(({ payload }) =>
concat(
this.service.addCrust(),
this.service.addTopings(),
this.service.sendToCook(),
this.service.checkOut()
)
)
)
);
但如果每个请求都依赖于前一个请求,则可以使用concatMap
如下运算符:
processOrder$ = createEffect(() =>
this.actions$.pipe(
ofType(OrderActions.PROCESS_ORDER),
concatMap(({ payload }) => this.service.addCrust(payload)),
concatMap((response1) => this.service.addTopings(response1)),
concatMap((response2) => this.service.sendToCook(response2)),
concatMap((response3) => this.service.checkOut(response3))
)
);
推荐阅读
- javascript - 将一个类传递给两个导航栏
- php - PHP:如何获取非对象的属性(错误消息 - 未定义的属性:stdClass::$0)
- react-native - 如何设置不换行的字幕
- javascript - jstree - 在 AJAX 完成后做一些事情
- vba - 预期:使用 Range 时的列表分隔符或 )
- python - 如何在 python 中使用循环有效地进行特征工程?
- c# - for 循环以某种方式覆盖了整个数组,即使我看不出有什么原因
- php - 我无法与 ReactPHP 的 Pawl 和 Ratchet 进行交叉通信
- php - 如何在 oop PHP 中将参数传递给父构造函数?
- c# - 使用多个参数(字符串、日期时间)的路由不起作用