0

I am able to create a new user in firebase Authentication like this:

const status = firebase.auth().createUserWithEmailAndPassword(email, password);   
console.log(status);

The console shows the output as this:

Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "rejected"
[[PromiseResult]]: FirebaseError: Firebase: The email address is badly formatted.

How would I get the PromiseState from my status variable?

  • You must read how to use [`async/await` or `Promise`](https://stackoverflow.com/questions/44512388/understanding-async-await-on-nodejs) – Ashish Mar 03 '22 at 13:44
  • Does this answer your question? [How can I synchronously determine a JavaScript Promise's state?](https://stackoverflow.com/questions/30564053/how-can-i-synchronously-determine-a-javascript-promises-state) – jarmod Mar 03 '22 at 13:44

1 Answers1

2

The promise state is whether the promise resolves or rejects. If you're using the .then approach to working with promises, you can supply a second callback function to handle the error case:

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(status => {
    // If it gets to here, the promise resolved
    console.log(status);
  }, error => {
    // If it gets to here, the promise rejected
    console.log(error);
  });

// Or, equivalently:
firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(status => {
    console.log(status);
  })
  .catch(error => {
    console.log(error);
  });

If you're using async/await, then you handle the error case with a try/catch

async someFunction() {
  try {
    const status = await firebase.auth().createUserWithEmailAndPassword(email, password);
    // If it gets to here, the promise resolved
    console.log(status);
  } catch (error) {
    // If it gets to here, the promise rejected
    console.log(error);
  }
}
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98