簡體   English   中英

如何在 firebase 使用 javascript 創建新的 Auth 用戶時獲取 promise state

[英]How to get the promise state when creating a new Auth user in firebase using javascript

我可以像這樣在 firebase 身份驗證中創建一個新用戶:

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

控制台顯示 output 如下:

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

我如何從status變量中獲取PromiseState

promise state 是 promise 是解析還是拒絕。 如果您使用.then方法來處理承諾,您可以提供第二個回調 function 來處理錯誤情況:

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);
  });

如果您使用的是異步/等待,那么您可以使用 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);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM