gpt4 book ai didi

TypeScript 嵌套异步函数

转载 作者:搜寻专家 更新时间:2023-10-30 21:33:18 25 4
gpt4 key购买 nike

profile.page.ts:

  username: string;
totalScore: number;
...


loadUserData() {
this.spinnerDialog.show();
this.firebaseServie.loadUserData().then(() => {
this.username = this.sessionData.getUser().getUsername();
this.totalScore = this.sessionData.getUser().getTotalScore();
....
});

firebase.service.ts:

async loadUserData() {
console.log(this.sessionData.getUser().getEmail());
this.userCollection = this.afs.collection('users', ref => ref.where('email', '==', this.sessionData.getUser().getEmail().toLowerCase()));

this.userDoc = this.afs.collection("users").doc(this.sessionData.getUser().getEmail().toLowerCase());

this.x = this.userDoc.valueChanges().subscribe(((item: User) => {
this.userLoadedUser = item;
console.log("Found user by email id " + this.sessionData.getUser().getEmail() + ":" + this.userLoadedUser.username);
this.sessionData.getUser().setUsername(this.userLoadedUser.username);
this.sessionData.getUser().setTotalScore(this.userLoadedUser.totalScore);
....

}));
}

那么我如何确定 then() 子句中的部分仅在我们从 firebase 获得数据后执行?

为了更好地理解,我已经编辑了我的问题。

最佳答案

您可以简单地在您的 firebase.service.ts 中返回一个新的 Promise:

loadUserData() {
return new Promise((resolve, reject) => {
...
this.x = this.userDoc.valueChanges().subscribe(((item: User) => {
...
resolve(); // If everything worked!
}));
...
});
}

希望对您有所帮助!

关于TypeScript 嵌套异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com