Created
August 10, 2012 15:46
-
-
Save briancavalier/3315140 to your computer and use it in GitHub Desktop.
A simple promise-based task queue
This file contains 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
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