Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Created November 4, 2010 15:49
Show Gist options
  • Save jakearchibald/662672 to your computer and use it in GitHub Desktop.
Save jakearchibald/662672 to your computer and use it in GitHub Desktop.
function asyncQueue(/* function, function, ... */) {
var slice = Array.prototype.slice,
funcs = slice.call(arguments, 0),
baseArgs = [function() {
next(arguments);
}],
next = function(args) {
var nextFunc = funcs.shift();
nextFunc && nextFunc.apply(
this, baseArgs.concat( slice.call(args, 0) )
);
};
next();
};
// eg...
asyncQueue(function(next) {
console.log('1');
setTimeout(function() {
next('hello', 'world');
}, 1000);
}, function(next, msg1, msg2) {
console.log('2');
console.log(msg1); // hello
console.log(msg2); // world
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment