redis-smq-common
Version:
RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.
35 lines • 1.23 kB
JavaScript
import { parallel } from './parallel.js';
export function map(items, operation, chunkSize, callback) {
if (items.length === 0) {
return callback(null, []);
}
const results = [];
const processChunk = (startIndex, chunkIndex = 1) => {
if (startIndex >= items.length) {
return callback(null, results);
}
const endIndex = Math.min(startIndex + chunkSize, items.length);
const chunkItems = items.slice(startIndex, endIndex);
parallel(chunkItems.map((item) => (cb) => {
try {
operation(item, (err, result) => {
if (err) {
return cb(err);
}
return cb(null, result);
});
}
catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
return cb(err);
}
}), (err, chunkResults) => {
if (err)
return callback(err);
results.push(...chunkResults);
setImmediate(() => processChunk(endIndex, chunkIndex + 1));
});
};
processChunk(0);
}
//# sourceMappingURL=map.js.map