Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Created August 10, 2012 15:46
Show Gist options
  • Save briancavalier/3315140 to your computer and use it in GitHub Desktop.
Save briancavalier/3315140 to your computer and use it in GitHub Desktop.
A simple promise-based task queue
var when = require('../when');
/**
* A simple promise-based task queue that runs tasks in sequence without
* overlap. Adding a task returns a promise that resolves when that task
* completes.
*/
function Queue() {}
Queue.prototype = {
/**
* Add a new task to this Queue
* @param task {Function}
* @param ... arguments with which to invoke the task
* @return {Promise} a promise that will resolve with the completion value of
* the task.
*/
add: function(task) {
var args = Array.prototype.slice.call(arguments, 1);
function applyTask() {
return task.apply(null, args);
}
this.current = when(this.current, applyTask, applyTask);
return this.current;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment