在 Node.js 中一次迭代 50 个项目的数组

问题描述 投票:0回答:4

我是 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
    });

任何帮助将不胜感激!

更新(回复回复)

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
                }
            })
        })
    )
}
javascript arrays node.js promise
4个回答
17
投票

您应该首先将数组分成 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);

使用 async/await 将在后台使用 Promise,因此

await
将等到当前块上传,然后才会上传下一个(如果没有发生错误)。

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

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

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

更新

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

更新2

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

function uploadDataChunk(chunk) {
  return Promise.all(
    chunk.map(uploadItemToGoogleCloud) // uploadItemToGoogleCloud should return a promise
  );
}

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.
.as-console-wrapper{
max-height: 100% ! important;
}


2
投票

有一个曾经非常流行的库:async.js(不要与

async
关键字混淆)。我仍然认为有时这是更干净的方法,尽管现在我倾向于在
async/await
循环中手动执行此操作。
异步库实现了许多异步流程控制设计模式。对于这种情况,您可以使用 

for

:

eachLimit

或者,如果您愿意,可以使用承诺版本,这样您就可以
const eachLimit = require('async/eachLimit'); eachLimit(result, 50, function (item) { // do your forEach stuff here }, function (err) { // this will be called when everything is completed } );

循环:

await

在这种特定情况下,手动批处理操作并使用 
const eachLimit = require('async/eachLimit'); async function processResult (result) { // ... try { await eachLimit(result, 50, function (item) { // do your forEach stuff here }); } catch (err) { // handle thrown errors } }

在批次之间暂停非常容易,但

await
库包含一组丰富的有用的函数。即使使用
async.js
,其中一些仍然很难做到,例如
async/await
(异步
whilst
)、
while
retry
等(请参阅文档:
https://caolan.github.io/异步/v3/docs.html


0
投票
forever

lodash
功能:
chunk

参考:
https://lodash.com/docs/#chunk

© www.soinside.com 2019 - 2024. All rights reserved.