javascript - 在 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 ;
};
解决方案
一般来说,将 async/await 与 then/catch 链结合在同一个 Promise 上并不是一个好主意。async/await 的全部意义在于允许不涉及使用 then/catch 嵌套回调的更具可读性的代码。
您的第二个选项是更惯用的 JavaScript。
推荐阅读
- php - Wordpress 插件:反向代理静态文件
- go - 编辑内存中的 zip 文件
- function - 我应该如何阅读有关递归的代码?
- selenium - Watir:在 Windows 而非 Linux 中使用 headless-chrome 下载
- java - 正则表达式检查小数是否在两个数字之间,而与小数点后的位数无关
- java - 使用 TestNG 框架使用 Selenium 跳过测试用例
- javascript - 如果溢出,HTML 内容到 PDF 不会添加页面 - jsPDF
- c# - 自动淡入淡出场景
- gerrit - 如何使用gerritnoteb,我想查询db中的一些评论记录
- javascript - 如何在javascript中使用json对象删除对象的json数组?