Skip to content

Instantly share code, notes, and snippets.

@aal89
Created July 4, 2019 09:53
Show Gist options
  • Save aal89/916b0992e13eb25c16101fb078b17eb0 to your computer and use it in GitHub Desktop.
Save aal89/916b0992e13eb25c16101fb078b17eb0 to your computer and use it in GitHub Desktop.
Batch queue javascript
function BatchQueue(concurrentTasks) {
this.concurrency = concurrentTasks;
this.running = 0;
this.queue = [];
}
BatchQueue.prototype.pushTask = function(task, callback) {
this.queue.push(task);
this.next();
}
BatchQueue.prototype.next = function() {
var self = this;
// while if were running and there are spots left and we have work
// todo, do it. Shift work off the queue and execute the task
while(self.running < self.concurrency && self.queue.length) {
var task = self.queue.shift();
task(function(err) {
self.running--;
self.next();
});
self.running++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment