将所有 firebase 用户列为承诺的代码审查

问题描述

这不是对我所写的内容进行更多代码审查的问题。

我尝试将 firebase 列表中的所有用户代码转换为 promise 函数。我想一次性检索所有用户的数组,而不是使用函数回调。

谁能告诉我这是否会因为使用太多内存或其他问题而中断。

我将用户批处理参数保留为 1 以表明异步似乎确实有效。

const userArray = [];

// list all users
const listAllUsers = function (nextPagetoken){
  return new Promise((resolve,reject) => {
    admin
      .auth()
      .listUsers(1,nextPagetoken)
      .then(async(listUsersResult) => {
        listUsersResult.users.forEach((userRecord) => {
          console.log('user',userRecord.email);
          userArray.push(userRecord.email);
        });
        if (listUsersResult.pagetoken) {
          console.log("There is a next page token")
          await listAllUsers(listUsersResult.pagetoken);
          resolve();
        } else {
          resolve();
        }
      })
      .catch((error) => {
        console.log('Error listing users:',error);
      });
  });
};

// Start listing users from the beginning,1000 at a time.
await listAllUsers();
  

解决方法

嗯……代码将使用至少与用户数量成线性关系的内存量。所以如果你对用户数量没有限制,你也无法预测它需要的内存量。

我立刻想到的问题是为什么需要所有用户的列表?你打算在整个列表上做什么,而你不能在一个用户页面上做?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...