0

I'm new with promises, and am stuck. I want to get data in the last then, and return a promise, cause i'm writing a function, that I'll use in some cases and do different stuff with returned data.

I want to return LAST .then((user) => Promise.resolve({ user, userObject })) as it is, to use it with then in places where I'll use function, but now I'm not ending with that statement, in case I'm loosing data in first .then statement. What's wrong?

UPDATE:

so here is the full function body. Now im using it like that:

signUpUser({ credentials: preparedForm })
  .then((t) => console.log('submitHandler -> t', t))
  .catch((e) => {
    handleSignUpErrors({
      form: signUpForm,
      updateForm: setSignUpForm,
      error: e,
      showErrors: setShowErrors,
    })
  })

But i'm not getting console.log. What's wrong?

const signUpUser = ({ credentials }) => {
  const {
    name,
    lastName,
    email,
    password,
    phone,
    emailVerified,
    phoneVerified,
  } = credentials

  const userObject = getUserObject({
    name,
    lastName,
    email,
    password,
    phone,
    emailVerified,
    phoneVerified,
  })

  return firebase
    .auth()
    .createUserWithEmailAndPassword(email, password)
    .then((data) =>
      db
        .collection('users')
        .doc(data.user.uid)
        .set(userObject)
        .then(() => data)
    )
    .then((user) => ({ user, userObject }))
}
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
WhoIsDT
  • 695
  • 2
  • 9
  • 27

2 Answers2

0

You don't need Promise resolve at all, just return all objects in .then, this should work for you.

  const userObject = getUserObject({
    name,
    lastName,
    email,
    password,
    phone,
    emailVerified,
    phoneVerified,
  })

  return firebase
    .auth()
    .createUserWithEmailAndPassword(email, password)
    .then((data) =>
      return db
        .collection('users')
        .doc(data.user.uid)
        .set(userObject)
        .then(() => data)
    )
    .then((user) => ({ user, userObject }))
Majid
  • 2,507
  • 2
  • 19
  • 18
  • I'v updated my post to get full picture, where and how i want to use data from returned function signUpUser. Please check it – WhoIsDT Jul 14 '20 at 09:56
0
const userObject = getUserObject({
  name,
  lastName,
  email,
  password,
  phone,
  emailVerified,
  phoneVerified,
})

return firebase
  .auth()
  .createUserWithEmailAndPassword(email, password)
  .then((data) =>
    db
    .collection('users')
    .doc(data.user.uid)
    .set(userObject)
    .then(response => response)
  )
  .then(result => console.log(result))
Sohan
  • 558
  • 6
  • 17
  • thats wrong. please check my question and update how and what i want to have as a result from function – WhoIsDT Jul 14 '20 at 09:56
  • Code only answers are often not helpful. Could you add an explanation on why/how your code fixes the problem in the question? – 3limin4t0r Jul 14 '20 at 10:02