I'm new to node.js and am currently trying to code array iterations. I have an array of 1,000 items - which I'd like to iterate through in blocks of 50 items at a time due to problems with server load.
I currently use a forEach loop as seen below (which I'm looking at hopefully transforming into the aforementioned block iteration)
//result is the array of 1000 items
result.forEach(function (item) {
//Do some data parsing
//And upload data to server
});
Any help would be much appreciated!
UPDATE (in reponse to reply)
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
}
})
})
)
}