ramirez/queue.js

26 lines
400 B
JavaScript
Raw Normal View History

2016-12-05 17:25:20 -05:00
class Queue {
constructor() {
this.running = false;
this.queue = [];
}
add(fn) {
this.queue.push(fn);
if (!this.running) this.next();
}
next() {
this.running = true;
if (this.queue.length === 0) {
this.running = false;
return;
}
const fn = this.queue.shift();
Promise.resolve(fn()).then(() => this.next());
}
}
module.exports = Queue;