如何在特定条件下兑现承诺?

Ahm*_*amy 4 javascript promise

我是JavaScript的新手,我真的对有关promise的文档感到困惑。我在以下情况下遇到了很多用户,并对每个用户执行一个异步函数,在该函数中我对该用户进行一些计算,并将结果与​​该用户一起添加到数组中。根据我从文档中了解到的信息,每次执行异步函数时,我都需要获得一个诺言,并将所有诺言添加到一个诺言列表中,这些诺言列表在将结果数组传递给它时会进行解析,如下所示:

 someFunction = () => {
   var promises = [];
   users.forEach(user => {
       var promise = asyncFunction(user).callback(callBackValue => {
         // Run some checks and add the user to an array with the result
         if (checksAreGood) {
           usersArray.push({user: user, result: callBackValue});
         }
       });
       promises.push(promise);
   });
   return Promise.all(promises).then(() => Promise.resolve(matches));
 };
Run Code Online (Sandbox Code Playgroud)

问题是:如果我正在遍历的用户数未知,并且仅当用户数大于20时,我想将添加到数组的用户数限制为20,否则添加所有用户。换句话说,当阵列中有20个或更少的用户时,请解决诺言。这样做的目的是避免对要优化性能的全部用户执行异步功能。意思是,如果我有1000个用户,我希望执行异步功能,直到数组只填满20个为止。

Jon*_*lms 5

仅搜索直到找到20个用户的第一个解决方案是遍历一个用户

 async function someFunction(){
  const results = [];
  for(const user of users){
     const result = await asyncFunction(user);
     // Run some checks and add the user to an array with the result
     if(!someChecksGood) continue;
     results.push(result);
     if(results.length >= 20) break;
  }
  return results;
 }
Run Code Online (Sandbox Code Playgroud)

尽管这是“完美的”工作,但它非常慢,因为一次只能处理一个请求。因此相反的解决方案是一次运行所有请求,如果阵列已满则取消它们:

 async function someFunction(){
  const results = [];
  async function process(user){
    const result = await asyncFunction(user);
    if(!someChecksGood || results.length >= 20) return;
    results.push(result);
   }
   await Promise.all(users.map(process));
   return results;
 }
Run Code Online (Sandbox Code Playgroud)

但是现在有大量不必要的请求,这些请求随后会被丢弃。为了改善这一点,可以通过“分批”请求来结合上述两种方法,这不应减少请求时间,因为dbs一次只能处理一定数量的请求,但好处是我们可以在出现以下情况时停止处理数组已满,并且不需要处理其余的“块”,因此平均而言,它应该比以上两种解决方案都要好:

  async function someFunction(){
    //Chunk the users
    const chunks = [], size = 5;
    for(var i = 0; i < users.length; i += size)
      chunks.push( users.slice(i, i + size));
    //the method to create the results:
    const results = [];
    async function process(user){
      const result = await asyncFunction(user);
      if(!someChecksGood || results.length >= 20) return;
      results.push(result);
    }
    //iterate over the chunks:
    for(const chunk of chunks){
      await Promise.all(chunk.map(process));
      if(results.length >= 20) break;
    }
    return results;
 }
Run Code Online (Sandbox Code Playgroud)