在 node.js 中一次以 50 个项目为单位迭代数组

Hen*_*ies 3 javascript arrays node.js promise

我是 node.js 的新手,目前正在尝试编写数组迭代代码。我有一个包含 1,000 个项目的数组 - 由于服务器负载问题,我想一次遍历 50 个项目的块。

我目前使用如下所示的 forEach 循环(我希望将其转换为上述块迭代)

   //result is the array of 1000 items

   result.forEach(function (item) {
     //Do some data parsing
     //And upload data to server
    });
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激!

更新(响应回复)

async function uploadData(dataArray) {
    try {
        const chunks = chunkArray(dataArray, 50);
        for (const chunk of chunks) {
            await uploadDataChunk(chunk);
        }
    } catch (error) {
        console.log(error)
        // Catch en error here
    }
}

function uploadDataChunk(chunk) {
    return Promise.all(
        chunk.map((item) => {
            return new Promise((resolve, reject) => {
               //upload code
                }
            })
        })
    )
}
Run Code Online (Sandbox Code Playgroud)

Yur*_*kym 7

您应该首先将数组拆分为 50 个块。然后您需要一个一个地发出请求,而不是一次。Promise 可以用于此目的。

考虑这个实现:

function parseData() { } // returns an array of 1000 items

async function uploadData(dataArray) {
  try {
    const chunks = chunkArray(dataArray, 50);
    for(const chunk of chunks) {
      await uploadDataChunk(chunk);
    }
  } catch(error) {
    // Catch an error here
  }
}

function uploadDataChunk(chunk) {
  // return a promise of chunk uploading result
}

const dataArray = parseData();
uploadData(dataArray);
Run Code Online (Sandbox Code Playgroud)

使用 async/await 将在幕后使用承诺,因此await将等到当前块被上传,然后才会上传下一个(如果没有发生错误)。

这是我对 chunkArray 函数实现的建议:

function chunkArray(array, chunkSize) {
  return Array.from(
    { length: Math.ceil(array.length / chunkSize) },
    (_, index) => array.slice(index * chunkSize, (index + 1) * chunkSize)   
  );
}
Run Code Online (Sandbox Code Playgroud)

注意:此代码使用 ES6 特性,因此最好使用 babel / TypeScript。

更新

如果您创建多个异步数据库连接,只需使用一些数据库池工具。

更新 2

如果你想异步更新所有的块,并且当块上传时开始上传另一个块,你可以这样做:

function uploadDataChunk(chunk) {
  return Promise.all(
    chunk.map(uploadItemToGoogleCloud) // uploadItemToGoogleCloud should return a promise
  );
}
Run Code Online (Sandbox Code Playgroud)


Red*_*edu 6

您可以按所需的块大小对数组进行分块,如下所示;

function chunkArray(a,s){ // a: array to chunk, s: size of chunks
  return Array.from({length: Math.ceil(a.length / s)})
              .map((_,i) => Array.from({length: s})
                                 .map((_,j) => a[i*s+j]));
}

var arr = Array(53).fill().map((_,i) => i); // test array of 53 items
console.log(chunkArray(arr,5))              // chunks of 5 items.
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper{
max-height: 100% ! important;
}
Run Code Online (Sandbox Code Playgroud)