对于注销请求,我希望在请求成功和失败时分派相同的操作。为此,我考虑使用finalize()运算符。
然而,我似乎不能让它工作,因为它似乎从来没有被执行过。
效果:
/**
* 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)
)),
))
)
);我对RxJS比较陌生,所以如果有任何帮助,我将不胜感激。
发布于 2021-08-18 17:13:35
finalize是一个副作用运算符,因此您不能从它返回更多的值。
如果只想用一些值结束一个可观察对象,可以使用endWith
numbers$.pipe(endWith(42));对于更复杂的场景,比如以另一个可观察对象结束,像concatWith这样的东西将是可行的:
numbers$.pipe(concatWith(of(42)));发布于 2021-08-18 16:30:33
由于您使用switchMap访问看似是HTTP的调用,因此在logout$拥有订阅者之前不会发生任何事情
在出现错误的情况下,您似乎没有在catchError中重新抛出错误。由于您捕获了finalize,因此不再有错误,这就是为什么在出错时不会调用它。
https://stackoverflow.com/questions/68835647
复制相似问题