0

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.

fscore
  • 2,567
  • 7
  • 40
  • 74
  • use switchMap, so that the previous request will be cancelled automatically when a second LOGIN_REQUEST action is fired. – Praveen Dec 11 '18 at 07:28

1 Answers1

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()
  );
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39