在Firebase中使用async / await时,我必须使用.then吗?>

问题描述 投票:0回答:1

正如标题所说,在异步/等待函数中时,我需要使用.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 ;
};

正如标题所说,在异步/等待函数中时,我需要使用.then。这两个setPersonList = async()=> {const personList = [];之间的有效方式是什么?等待这个...。

javascript firebase async-await
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.