Skip to content

Instantly share code, notes, and snippets.

@goliatone
Created October 4, 2014 23:27
Show Gist options
  • Save goliatone/fcaa76e9bc302c511d29 to your computer and use it in GitHub Desktop.
Save goliatone/fcaa76e9bc302c511d29 to your computer and use it in GitHub Desktop.
Simple queue
define('helpers/queue', function() {
var Queue = {};
Queue.items = [];
Queue.enqueue = function(process) {
var id = Queue.items.push(process);
setTimeout(function() {
Queue.update(id);
}, 0);
};
Queue.update = function update(id) {
if (Queue.items.length < 1 || this.id) return;
var process = Queue.items.shift();
if (!process) return console.log('process no process');
this.id = id;
process(function() {
this.id = null;
this.update();
}.bind(this));
};
return Queue;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment