簡體   English   中英

動作分派時中止 promise 鏈 (rxjs)

[英]Abort promise chain upon action dispatch (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...
    })
);

希望我已經充分傳達了所需的邏輯。 我假設這兩者需要組合成一個 promise 鏈,但我不確定如何做到這一點。

我不是 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM