UNPKG

exec-queue

Version:

Safely run exec in a loop.

29 lines (24 loc) 582 B
const MAX_PROCS = 10; var exec = require('child_process').exec; var queue = []; function nextInLine () { console.log('queue length: ', queue.length); if (queue.length > 0) { var args = queue.shift(); exec.apply(null, args); } }; module.exports = function () { var args = Array.prototype.slice.call(arguments); var oldCb = args.pop(); args.push(function () { oldCb.apply(null, arguments); nextInLine(); // deque }); queue.push(args); // enque if (queue.length < MAX_PROCS) { process.nextTick(function () { nextInLine(); }); } };