问题
I'm trying to add in canceling in my request using redux-observable. Trying to implement a simple login example. Currently, I am unable to submit the login request again after I added the cancelation.
const loginUserApiCall = (username, password) => {
return new Promise((resolve, reject) => {
if (username === "foo" && password === "foo") {
resolve({
user: { name: "foo", lastName: "fooLName", email: "foo@gmail.com" }
});
}
reject({ err: "Creds are incorrect" });
});
};
const loginRequestEpic = (action$, state$) =>
action$.pipe(
ofType(LOGIN_REQUEST),
mergeMap(action => {
const { username, password } = action.payload;
return loginUserApiCall(username, password);
}),
mergeMap(res => of(loginSuccess(res))),
takeUntil(action$.pipe(ofType(LOGIN_CANCELLED))),
catchError(err => {
return of(loginFailure(err))
})
);
What am I doing wrong as cancelation is not happening and I can't retry a request after canceling it? Once user cancels I should be able to retry again.
回答1:
Once takeUntil
is executed it'll complete your observable, so is when there are error thrown and the whole stream is deactivated. You can add repeat
operator to the end
action$.pipe(
ofType(LOGIN_REQUEST),
mergeMap(action => {
const { username, password } = action.payload;
return loginUserApiCall(username, password);
}),
mergeMap(res => of(loginSuccess(res))),
takeUntil(action$.pipe(ofType(LOGIN_CANCELLED))),
catchError(err => {
return of(loginFailure(err))
}),
repeat()
);
来源:https://stackoverflow.com/questions/53508804/cancel-request-using-redux-observable-is-not-working