Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript task queue
var queue = function() {
var tasks;
return {
start: function( list ) {
tasks = list;
first = tasks.pop();
first();
},
next: function() {
if ( tasks.length > 0 ) {
tasks.pop()();
}
}
}
}
// example implementation
task_fn = function(i) {
return function() {
console.log( "started " + i );
window.setTimeout( done_fn(i), 1000 );
}
}
done_fn = function(i) {
return function() {
console.log( "finished " + i );
q.next();
}
}
var
q = queue(),
todo = [ task_fn( 'a' ), task_fn( 'b' ), task_fn( 'c' ) ];
q.start( todo );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.