Skip to content

Instantly share code, notes, and snippets.

@supernifty
Created July 17, 2011 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supernifty/1087170 to your computer and use it in GitHub Desktop.
Save supernifty/1087170 to your computer and use it in GitHub Desktop.
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