我是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
}
})
})
)
}
答案 0 :(得分:3)
首先应该将数组拆分为50个块。然后你需要逐个发出请求,而不是一次。承诺可以用于此目的。
考虑这个实现:
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 en error here
}
}
function uploadDataChunk(chunk) {
// return a promise of chunk uploading result
}
const dataArray = parseData();
uploadData(dataArray);
使用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)
);
}
注意:此代码使用ES6功能,因此最好使用babel / TypeScript。
如果您创建多个异步数据库连接,只需使用一些数据库池工具。
如果要异步更新所有块,并且当上传块时开始上传另一块,您可以这样做:
function uploadDataChunk(chunk) {
return Promise.all(
chunk.map(uploadItemToGoogleCloud) // uploadItemToGoogleCloud should return a promise
);
}
答案 1 :(得分:1)
您可以按照以下步骤将数组块化为所需的块大小;
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 :(得分:0)
有一个曾经非常流行的库:async.js(不要与 Mon, 05 Jul 2021 23:19:58 IST
关键字混淆)。我仍然认为有时使用 async
是更简洁的方法,但我倾向于在 async/await
循环中手动执行此操作。
异步库实现了许多异步流控制设计模式。对于这种情况,您可以使用 for
:
eachLimit
或者,如果您愿意,您可以使用promisified 版本,以便您可以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
等(请参阅文档:{ {3}})