@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
35 lines • 1.25 kB
JavaScript
import { runCommand } from './runner.js';
import { runnerPolicySchema } from './types.js';
export const executeCommands = (commands, policyInput = {}) => {
const policy = runnerPolicySchema.parse(policyInput);
if (commands.length === 0) {
return Promise.resolve([]);
}
return new Promise((resolve, reject) => {
const results = new Array(commands.length);
let nextIndex = 0;
let active = 0;
const dispatch = () => {
while (active < policy.maxConcurrency && nextIndex < commands.length) {
const capturedIndex = nextIndex++;
active++;
runCommand(commands[capturedIndex], policy)
.then((result) => {
results[capturedIndex] = result;
})
.catch(reject)
.finally(() => {
active--;
if (nextIndex < commands.length) {
dispatch();
}
else if (active === 0) {
resolve(results);
}
});
}
};
dispatch();
});
};
//# sourceMappingURL=execute-commands.js.map