在firebase中使用async/await时,我必须使用.then吗



正如标题所说,当在async/await函数内部时,我需要使用.then。两个之间的有效方式是什么

setPersonList = async ()=> {
const personList = [];
await this.firestoreCollection
.get()
.then(result => {
personList  = { ...result.data };
});
return personList ;
};

setPersonList = async () => {
const personList = [];
const snapshot = await this.firestoreCollection
.get()
snapshot.docs.forEach((doc) => {
personList .push(doc.data());
});
return personList ;
};

通常,在同一个promise上组合async/await和then/catch链不是一个好主意。async/await的全部目的是允许更可读的代码,而不涉及使用then/catch嵌套回调。

您的第二个选项是更惯用的JavaScript。

最新更新