使用 async/await 时函数未按正确顺序触发

在 Angular 服务中,我创建了以下函数:


getListKey(user) {

  firebase.database().ref(`userprofile/${user.uid}/list`).once('value').then(snapshot => {

    console.log(snapshot.val())

    this.listKey = snapshot.val()

    return this.listKey

  })

}

我想在加载时在另一个文件中调用这个函数,并将返回的值分配给listKey服务中的全局变量,以用于组件中的另一个函数。但是,即使使用async/检索数据,第二个函数也会触发await。


这是我的组件中的相关部分:


this.afAuth.authState.subscribe(async (user: firebase.User) => {

  await this.fire.getListKey(user);

  this.fire.getUserList(this.fire.listKey).subscribe(lists => {...})

  ...

}

我怎样才能getUserList()等待listKey?


呼唤远方
浏览 184回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript