redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
38 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.map = map;
const parallel_js_1 = require("./parallel.js");
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);
(0, parallel_js_1.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