将 Firebase 方法转换为异步等待

joh*_*ohn 0 javascript firebase firebase-authentication google-cloud-functions google-cloud-firestore

我有这个 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
        })
      })
  }
Run Code Online (Sandbox Code Playgroud)

它还使用获取用户详细信息和用户类型的云功能将用户添加到 Firestore 集合中。

我有3个承诺(

  1. createUserWithEmail...()
  2. updateUserProfile()1
  3. createUser()

) 彼此依赖.. 如何在一个函数中使用它们?

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

如何在不使用 的情况下编写此方法.then()?一些用户没有出现在数据库中

Jar*_*a X 6

删除.then只需使用await“更好”

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
    });
}
Run Code Online (Sandbox Code Playgroud)