简体   繁体   中英

How can break generator function by throw error inside redux saga in react

I have this function inside redux-saga

function* CheckUserIsAuth() {
  try {
    const user = yield call(getCurrentUser);
    console.log(user);
    if (!user) throw new Error(null);
    const data = yield call(createUserDocument, user);
    const dataSnapshot = yield data.get();
    yield put(emailSignInSucc({ id: dataSnapshot.id, ...dataSnapshot.data() }));
  } catch (error) {
    yield put(emailSignInError(error.message));
  }
}

I want to break the try and catch error if user is null

I tried to throw error using the code above but it still went to emailSignInSucc and gave me null as string.

That means it complete function and does not break it

Check user and what they are returning. Perhaps user is an empty object and the error is not triggered.

If throw new Error () is called then the code will continue to run in catch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM