kalipts
kalipts

Reputation: 1317

Redux saga yield call not waiting with firebase?

I create a saga to update user information. But yield console.log('waiting'); not waiting util my Fibase update complete

export function* handleUpdateUserInfo(action) {
  yield put(updateUserInfoPending());
  const { userInfo, userID } = action;
  const currentUser = yield select(state => state.user.currentUser);
  const newUser = {
    ...currentUser,
    ...userInfo
  };

  try {
    yield call(() => updateUserProfile(newUser, userID));
    yield console.log('waiting');
  } catch (error) {
    yield put(updateUserInfoFailure(error));
  }
}

Firebase

export const updateUserProfile = (newUser, userID) => {
  try {
    firestore
      .collection('users')
      .doc(userID)
      .set(newUser);
  } catch (error) {
    throw error;
  }
};

I want to ask how I fix it? and why it happen

Upvotes: 0

Views: 275

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598887

If you return the call from updateUserProfile, you can await its result in the caller.

export const updateUserProfile = async (newUser, userID) => {
  return firestore
      .collection('users')
      .doc(userID)
      .set(newUser);
};

Then you call it with:

yield call(() => await updateUserProfile(newUser, userID));
yield console.log('waiting');

Upvotes: 1

Related Questions