[英]Do I have to use .then when using async/await in firebase
Just as the title says do I need to use.then when inside a async/await function.正如标题所说,我需要使用.then 在 async/await function 中时。 What's the efficient way between the two两者之间的有效方式是什么
setPersonList = async ()=> {
const personList = [];
await this.firestoreCollection
.get()
.then(result => {
personList = { ...result.data };
});
return personList ;
};
or或者
setPersonList = async () => {
const personList = [];
const snapshot = await this.firestoreCollection
.get()
snapshot.docs.forEach((doc) => {
personList .push(doc.data());
});
return personList ;
};
In general, it's not a good idea to combine async/await with then/catch chains on the same promise.一般来说,在同一个 promise 上将 async/await 与 then/catch 链结合起来并不是一个好主意。 The whole point of async/await is to allow for more readable code that doesn't involve nesting callbacks with then/catch. async/await 的全部意义在于允许不涉及使用 then/catch 嵌套回调的更具可读性的代码。
Your second option is much more idiomatic JavaScript.您的第二个选择是更惯用的 JavaScript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.