在动作分派时中止承诺链 (rxjs)

问题描述

这是我想要实现的目标:

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
    );

这是我“尝试过”的:

this.actions$.pipe(
    ofActiondispatched(ABORT)
    .subscribe(() => {
        // somehow interrupt the promise chain in the above code block...
    })
);

希望我已经充分传达了所需的逻辑。我认为这两个需要合并到一个承诺链中,但我不确定如何做到这一点。

解决方法

我不是 100% 确定你的 ofActionDispatched 函数是如何工作的,但是如果它需要中止然后使用 catchError 返回一个空值,你能让它抛出一个错误,然后在订阅中检查它,就像这样:

this.actions$.pipe(
            map(obj => {
                // do stuff
            }),catchError(err => {
                return of(null);
            }))
        .subscribe((res) => {
                if (!res) {
                    // do stuff if error was caught in pipe
                }
            })