[英]Abort promise chain upon action dispatch (rxjs)
Here is what I want to achieve:这是我想要实现的目标:
this.jobManager
.queue(
// start a job
)
.then(
// do more stuff, but abort if `ABORT` action dispatched before it finishes
)
.finally(
// still do some cleanup even if `ABORT` dispatched
);
Here is what I have "tried":这是我“尝试过的”:
this.actions$.pipe(
ofActionDispatched(ABORT)
.subscribe(() => {
// somehow interrupt the promise chain in the above code block...
})
);
Hopefully, I have sufficiently communicated the required logic.希望我已经充分传达了所需的逻辑。 I would assume the two would need to be combined into a single promise chain but am unsure how to do this.我假设这两者需要组合成一个 promise 链,但我不确定如何做到这一点。
I'm not 100% sure how your ofActionDispatched function is working but can you get it to throw an error if it needs to abort and then use catchError to return a null value and then check for that in the subscription, like so:我不是 100% 确定你的 ofActionDispatched function 是如何工作的,但是如果它需要中止,你能不能让它抛出一个错误,然后使用 catchError 返回一个 null 值,然后在订阅中检查它,如下所示:
this.actions$.pipe(
map(obj => {
// do stuff
}),
catchError(err => {
return of(null);
}))
.subscribe((res) => {
if (!res) {
// do stuff if error was caught in pipe
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.