Skip to content

Instantly share code, notes, and snippets.

@jcorbin
Created March 15, 2014 23:59
Show Gist options
  • Save jcorbin/9575840 to your computer and use it in GitHub Desktop.
Save jcorbin/9575840 to your computer and use it in GitHub Desktop.
var isFunction = require('lodash.isFunction');
function oneAtATime(func) {
var running = false;
var queue = [];
function done() {
for (var i=0, n=queue.length; i<n; i++)
queue[i].apply(this, arguments);
running = true;
queue = [];
}
return function() {
var args = Array.prototype.slice.call(arguments);
var callback = args.slice(-1)[0];
if (isFunction(callback)) {
queue.push(callback);
args = args.slice(0, -1);
}
if (!running) {
running = true;
args.push(done);
func.apply(this, args);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment