A parallel, asynchronous implementation of forEach in ES6 with a configurable maximum degree of parallelism.
npm install parallel-eachnpm i parallel-each
peach(array, callback, chunkSize); - Array, Function, (optional) Number - returns any items (and their indexes and error reasons) whose callbacks may have errored.
callback(item, index, array) - callback to process each item of array.
chunkSize - maximum degress of parallelism in which to process array.
const peach = require('parallel-each');
const asyncFn = () => new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1);
});
var collection = [];
for (var i = 0; i < (Math.pow(10, 3)); i++) {
collection.push({
index: i,
item: item_${i}
});
}
var actualSum = collection.length * (collection.length + 1) / 2;
var sum = 0;
peach(collection, async (_, i) => {
await asyncFn();
sum += i + 1;
}, 10).then(() => {
var processed = (sum === actualSum); // true
});
`
`
const peach = require('parallel-each');
const asyncFn = () => new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 1);
});
const asyncErrorFn = () => new Promise((_, reject) => {
setTimeout(() => {
reject(new Error());
}, 1);
});
var collection = [];
for (var i = 0; i < (Math.pow(10, 3)); i++) {
collection.push({
index: i,
item: item_${i}
});
}
var actualSum = collection.length * (collection.length + 1) / 2;
var sum = 0;
peach(collection, async (_, i) => {
if (i % 2 === 0) {
await asyncErrorFn();
}
await asyncFn();
sum += i + 1;
}, 10).then((erroredItems) => {
var errored = (erroredItems.length === collection.length / 2); // true
var processed = (sum === (actualSum / 2 + (collection.length / 4))); // true
});
``