Skip to content

Instantly share code, notes, and snippets.

@gitawego
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save gitawego/b8be9438513fe043b234 to your computer and use it in GitHub Desktop.

Select an option

Save gitawego/b8be9438513fe043b234 to your computer and use it in GitHub Desktop.
task buffer method
function queue(tasks, scope) {
var slice = Array.prototype.slice,task, on ={}, error;
function next() {
if (task = tasks.shift()) {
task.apply(scope, [next].concat(slice.call(arguments, 0)));
}else{
on.done && on.done();
}
}
next.error = function(err){
error = err || 'unknown error';
on.error && on.error(err,tasks);
}
next.apply(scope, slice.call(arguments, 2));
return {
on:function(evtName,fnc){
on[evtName] = fnc;
if(error){
on.error && on.error(error,tasks);
} else if(tasks.length === 0){
on.done && on.done();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment