gpt4 book ai didi

javascript - 在 firebase 中使用 async/await 时是否必须使用 .then

转载 作者:行者123 更新时间:2023-12-02 20:53:58 26 4
gpt4 key购买 nike

正如标题所说,在 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。

关于javascript - 在 firebase 中使用 async/await 时是否必须使用 .then,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61535443/

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