Manuel Brás
Manuel Brás

Reputation: 513

RxJS - Finalize not firing

For a logout request, I want to dispatch the same actions when the request is successful as well as when it fails. For this, I thought about using the finalize() operator.

However, it seems I can't get it working, as it appears to never get executed.

The effect:

/**
 * When the user logs out :
 * Try to delete its push token
 * Clear user
 */
logout$ = createEffect(() =>
    this.actions$.pipe(
        ofType(ClassicAuthActions.logout),
        switchMap(action => this.api.logout().pipe(
            map(() => ClassicAuthActions.logoutDidSuccess()),
            catchError(err => of(ClassicAuthActions.logoutDidFail())),
            finalize(() => of(
                    ClassicAuthActions.deleteUserCommunicationToken(),
                    ClassicAuthActions.clearUser(action.redirect)
            )),
        ))
    )
);

I'm rather new to RxJS so any help would be appreciated.

Upvotes: 2

Views: 2612

Answers (2)

Ingo Bürk
Ingo Bürk

Reputation: 20043

finalize is a side effect operator, so you cannot really return further values from it.

If you just want to end an observable with some value, you can use endWith:

numbers$.pipe(endWith(42));

For more complex scenarios like ending with another observable, something like concatWith would be the way to go:

numbers$.pipe(concatWith(of(42)));

Upvotes: 4

Bertramp
Bertramp

Reputation: 385

Since you switchMap to what appears to be an HTTP call, nothing will happen until logout$ has a subscriber

In the case of an error you don't appear to be rethrowing the error in the catchError. Since you caught finalize there is no error anymore which is why it is not called on error.

Upvotes: 0

Related Questions