chrispalmo
chrispalmo

Reputation: 247

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.

Upvotes: 0

Views: 131

Answers (1)

momargoh
momargoh

Reputation: 66

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:

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

Upvotes: 1

Related Questions