如何根据特定条件解决承诺?

时间:2017-12-25 09:08:39

标签: javascript promise

我是JavaScript的新手,我真的对有关承诺的文档感到困惑。 我在这里有以下情况,我有一小部分用户和每个用户执行异步功能,我在该功能中对该用户进行一些计算,并将结果与​​用户一起添加到数组中。根据我从文档中所理解的,每次执行异步函数时我需要获得一个承诺,并将所有承诺添加到一个promises列表中,这些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));
 };

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

1 个答案:

答案 0 :(得分: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;
 }

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

 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;
 }

但现在有大量不必要的请求,之后被丢弃。为了改善这一点,可以通过“分块”请求来结合上述两种方法,这不应该减少请求时间,因为dbs一次只能处理一定数量的请求,但好处是我们可以在停止时处理数组已满,只有“chunk”的其余部分是不必要的,所以平均来说它应该比上面的两个解决方案都要好:

  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;
 }
相关问题