Javascript 将Firebase方法转换为异步等待

Javascript 将Firebase方法转换为异步等待,javascript,firebase,google-cloud-firestore,firebase-authentication,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Firebase Authentication,Google Cloud Functions,我有这个firebase方法,它使用电子邮件和密码创建firebase用户 async register(name, email, password,type) { let id; const createUser = this.functions.httpsCallable('createUser'); return await this.auth.createUserWithEmailAndPassword({email,password }) .the

我有这个firebase方法,它使用电子邮件和密码创建firebase用户

async register(name, email, password,type) {
    let id;
    const createUser = this.functions.httpsCallable('createUser');

    return await this.auth.createUserWithEmailAndPassword({email,password })
      .then((newUser)=>{
        id = newUser.user.uid;
        newUser.user.updateProfile({
          displayName: name
        })
      })
      .then(()=>{
        createUser({
          id:id,
          name:name,
          email:email,
          type:type
        })
      })
  }
它还使用获取用户详细信息和用户类型的云函数将用户添加到firestore集合

我有三个承诺

createUserWithEmail。。。 updateUserProfile1 createUser 它们相互依存。。如何在一个函数中使用它们

注意:由于用户类型字段,无法使用functions.auth.user.onCreate方法

我如何在不使用.then的情况下编写此方法? 有些用户没有出现在数据库中以删除。只需使用wait better即可


如果我没有弄错的话,有些用户不在DB中,所以它没有输入.then子句,这就是问题所在。如果我对它的描述是正确的,那么解决方法就是使用.catch子句,它将进入一个被拒绝的承诺。如果代码不在then子句中,则表示承诺被拒绝,请使用.catchreason来处理它。此外,在代码中,将aync WAIT语法与本机承诺混合使用。等待暂停函数的执行,直到承诺被解决或拒绝。您甚至不需要。那么,如果您使用wait.updateProfile,您总是在updateProfile之后调用createUser,这似乎很奇怪-这两个函数都是异步的吗?它们都返回承诺吗?谢谢。。。我如何在一个承诺中添加一个故障保护并拒绝所有承诺?如果您需要一个故障保护,我会坚持使用。然后在承诺链的末尾添加一个。catch,您可以用try catch来包装等待。它更具可读性,try-catch将捕获被拒绝的承诺作为未处理的承诺拒绝错误,并且您将丢失拒绝承诺的原始原因。而且,一个捕获就足够了,因为if在一个承诺链之后-一个.catch将处理所有拒绝。@丹尼斯,我想你的意思是其中一个承诺失败,然后拒绝所有承诺?如果是这样,请将所有承诺封装在一个承诺中。register函数返回一个承诺,然后对于每个承诺使用。然后。catch和catch集团内部拒绝包装承诺。谢谢最后一个问题:return语句会如何影响该方法?我将把它放在哪里?
async register(name, email, password,type) {
    let id;
    const createUser = this.functions.httpsCallable('createUser');

    const newUser = await this.auth.createUserWithEmailAndPassword({email,password });
    id = newUser.user.uid;
    // assuming the next two functions are asynchrnous AND return a promise
    // if not, just remove await
    await newUser.user.updateProfile({displayName: name});
    await createUser({
        id:id,
        name:name,
        email:email,
        type:type
    });
}