简体   繁体   English

动作分派时中止 promise 链 (rxjs)

[英]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.

相关问题 使用 setTimeout function 从 redux 分派一个动作并使用 fetch 调用解决 promise 链 - working with setTimeout function that dispatch an action from redux and resolve a promise chain with fetch call 递归承诺链末尾的链动作 - Chain action at the end of a recursive promise chain 如何在Redux中从诺言中调度动作? - How to dispatch an action from a promise in redux? 如果我的操作创建者返回 promise,Redux 何时解决调度? - When does Redux resolve a dispatch if my action creator return promise? 未在Promise的“ then”部分内进行动作分派(React / Redux) - An action dispatch inside the 'then' part of a Promise is not being called (React/Redux) 使用Promise.all进行多个API调用并调度操作 - Multiple API calls with Promise.all and dispatch an action 通过Promise.Dispatch Redux Action.all回调多个API端点 - Dispatch Redux Action by Promise.all callback several API Endpoint Redux Promise中间件如何反应将结果操作发送到调度? - How does react redux promise middleware send the resulting action to the dispatch? 如何让redux派遣一个动作并返回一个承诺? - How can I make redux dispatch an action as well as return a promise? 带有每个承诺呼叫的扁平承诺链系列,取决于先前呼叫的分辨率 - Flat promise chain series with each promise call dependent upon resolution of prior call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM