Cancel request using redux observable is not working

谁都会走 提交于 2019-12-24 18:55:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!