使用前获取用户名

当我单击“登录”按钮时,需要从状态获取用户名。单击按钮后,该名称将被写入状态,但是我没有时间捕获它,并且向请求发送了一个空字符串。如何延迟接收名称,将其写入变量然后使用?

 export const getGuestBalance = username => getGuestPaymentsHistory(username, {});

 export const beaxyLogin = (userData, bxySessionData) => (dispatch, getState, { API }) => {
  const state = getState();
  const username = state.auth.name;

  const getBalance = getGuestBalance(username);

  return dispatch({
    type: LOGIN,
    payload: new Promise(async (resolve, reject) => {
      try {
        const userMetaData = await waivioAPI.getAuthenticatedUserMetadata(userData.user.name);
        API.saveGuestData(
          userData.token,
          userData.expiration,
          userData.user.name,
          'beaxy',
          bxySessionData,
          () => dispatch(initBrokerConnection({ platform: 'beaxy', isBeaxyAuth: true })),
        );
        dispatch(getNotifications(userData.user.name));
        resolve({
          account: userData.user,
          userMetaData,
          socialNetwork: 'beaxy',
          isGuestUser: true,
          guestBalance: getBalance,
        });
      } catch (e) {
        dispatch(notify(e.error.details[0].message));
        reject(e);
      }
    }),
    meta: {
      refresh: getIsLoaded(state),
    },
  }).catch(e => {
    console.warn(e);
    dispatch(loginError());
    return e;
  });
};
评论