function handleProfileWatchResponse(
  dispatch,
  firebase,
  userProfileSnap,
  token
) {
  const {
    profileParamsToPopulate,
    autoPopulateProfile,
    useFirestoreForProfile,
    logErrors
  } = firebase._.config
  const profile = getProfileFromSnap(userProfileSnap)
  if (
    !profileParamsToPopulate ||
    useFirestoreForProfile || // populating profile through firestore not yet supported
    (!Array.isArray(profileParamsToPopulate) &&
      !isString(profileParamsToPopulate))
  ) {
    if (useFirestoreForProfile && profileParamsToPopulate) {
      console.warn('Profile population is not yet supported for Firestore') // eslint-disable-line no-console
    }
    dispatch({
      type: actionTypes.SET_PROFILE,
      profile: token ? { ...profile, token } : profile
    })
  } else {
    // Convert array of populate config into an array of once query promises
    promisesForPopulate(
      firebase,
      userProfileSnap.key,
      profile,
      profileParamsToPopulate
    )
      .then((data) => {
        // Fire actions for placement of data gathered in populate into redux
        forEach(data, (result, path) => {
          dispatch({
            type: actionTypes.SET,
            path,
            data: result,
            timestamp: Date.now(),
            requesting: false,
            requested: true
          })
        })
        if (!autoPopulateProfile) {
          // Dispatch action with profile combined with populated parameters
          dispatch({
            type: actionTypes.SET_PROFILE,
            profile: token ? { ...profile, token } : profile
          })
        } else {
          // Auto Populate profile
          const populates = getPopulateObjs(profileParamsToPopulate)
          const profile = userProfileSnap.val()
          dispatch({
            type: actionTypes.SET_PROFILE,
            profile: populate(
              { profile: token ? { ...profile, token } : profile, data },
              'profile',
              populates
            )
          })
        }
      })
      .catch((err) => {
        if (logErrors) {
          // eslint-disable-next-line no-console
          console.error(
            `RRF: Error retrieving data for profile population. Firebase:`,
            err
          )
        }
        // Error retrieving data for population onto profile.
        dispatch({
          type: actionTypes.UNAUTHORIZED_ERROR,
          authError: `Error during profile population: ${err.message}`
        })
        // Update profile with un-populated version
        dispatch({ type: actionTypes.SET_PROFILE, profile })
      })
  }
}
Handle response from profile listener. Works with both Real Time Database
and Cloud Firestore.
@param {Function} dispatch - Action dispatch function
@param {object} firebase - Internal firebase object
@param {firebase.database.Snapshot|firebase.firestore.DocumentSnapshot} userProfileSnap - Snapshot from profile watcher
@param {string} token - Token to pass along in action dispatch
@private