Created
July 4, 2019 09:53
-
-
Save aal89/916b0992e13eb25c16101fb078b17eb0 to your computer and use it in GitHub Desktop.
Batch queue javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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