异步函数返回Promise <Pending>

门迪·斯特伦菲尔德

我正在为一个项目构建一个Crud应用程序,人们可以添加视图和删除条目,为此我正在使用nodejs,js和fireabse。我有这样一个firestore数据库结构:

entries:
   --entry id
      --entry data
users:
   --user id
      --user email (query)
      -- my entries (collection)
           -- entries id
              --entry id 

现在我想显示所有用户条目,所以我创建了这个module.exports函数:

module.exports = {  
  //get all the users entries from database with passed in uid ('/dashboard')
  getUserEntries: async uid => {
    //get all his entries docs id from "myEntries" collection in an, array
    const usersEntriesId = await db
      .collection("users")
      .doc(uid)
      .collection("myEntries")
      .get()
      .then(entries => {
        return entries.docs.map(entry => entry.data().entry);
      })
      .catch(err => console.log(err));
    console.log(usersEntriesId); //works fine, logs array with ids

    const userEntriesDocs = usersEntriesId.map(async id => {
      const entry = await db
        .collection("entries")
        .doc(id)
        .get()
        .then(entry => {
          return entry.data();
        });
      console.log("hello:", entry); //works fine returns logs entry data

      return entry;
    });

    console.log("hello1: ", userEntriesDocs); //doesnt work i get "hello1:  [ Promise { <pending> },
 // Promise { <pending> },
  //Promise { <pending> } ]"
//i got three entries, that's why i get 3 times "Promise { <pending> }"
  }
};

那么我该如何解决呢?

谢谢

天空男孩

好了,async函数返回了Promise,它是如何在后台运行的。如果没有.map,则可以仅await使用该函数,或者将其Promise.then()and任意使用.catch但是由于存在阵列,Promise您需要Promise.all等待所有问题解决。

const userEntriesDocs = await Promise.all(usersEntriesId.map(async id => {
....
);

当心:与有所不同.allSettled.all()如果后续任何失败将立即Promise失败。因此,如果出于任何原因想要从成功的请求中获取数据,则需要更复杂的逻辑。

或者,您可以手动进行循环:

const userEntriesDocs = [];
for(const docPromise of userEntriesId.map(.....)) {
  userEntriesDocs.push(await docPromise);
}

但是对我await Promise.all[...]来说更具可读性。

我还要强调一下,这里有很多Promises(请求已经发送)。如果您尝试在循环内发送请求例如

const userEntriesDocs = [];
for(const id of userEntriesId) {
  userEntriesDocs.push(await db
        .collection("entries")
        .doc(id)
        .get()
        .then(entry => {
          return entry.data();
         })
  );
}

您会发现请求严格按照一对一进行,而不是并行进行。这将需要更多时间来处理列表。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

异步/等待返回Promise {<pending>}

为什么我的异步函数返回Promise {<pending>}而不是值?

为什么我的异步函数返回Promise {<pending>}而不是值?

useState与异步函数一起返回Promise {<pending>}

获取函数返回Promise <pending>

返回异步/等待错误Promise {<pending>}

使用Express异步/等待返回Promise {<pending>}

为什么异步函数在控制台中返回Promise <pending>错误

为什么我的函数返回 Promise { <pending> }

一旦函数返回Promise {<pending>}

promise.then() 返回 Promise { <pending> }

Promise { <pending> } 在 node js 异步函数上使用 map

异步等待返回承诺<pending>

async / await返回Promise {<pending>}

node.js 函数返回 Promise { <pending> },为什么?

MongoDB:cursor.toArray返回Promise {<pending>}

为什么Promise在.then之后返回为<Pending>?

MongoDB与nodejs的连接返回promise <pending>

尝试检索 JSON 数据会导致 [ Promise { <pending> }, Promise { <pending> } ]

为什么这个箭头函数返回一个 Promise{<pending>}

返回Promise Pending及其已满,但不返回json

等待的承诺仍然返回<pending>

为什么函数 getSafestCountriesNames() 在我调用它时返回 promise{pending} 而当我使用 async/await 时它返回 undefined?

为什么 Vuex 的 action 返回一个 promise<pending>?

HTTP请求的单元测试返回Promise {<pending>}

我的通话async / await在我的操作中返回Promise {<pending>}

我想用 axios 的返回全局状态,但是 Promise { <pending> }

使用 mongoose find() 函数时,我收到 Promise <Pending>

返回promise的函数必须是异步的