Skip to content

Instantly share code, notes, and snippets.

@tdantas
Created February 3, 2016 15:25
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 tdantas/24840d1e6f6c4c9c2750 to your computer and use it in GitHub Desktop.
Save tdantas/24840d1e6f6c4c9c2750 to your computer and use it in GitHub Desktop.
delaying the execution of your function until ready be called
const _ = require('lodash');
module.exports = queue;
function queue(fn) {
var queuedArgs = [];
var ready = false;
queuedFn.ready = function() {
ready = true;
queuedArgs.forEach((arg) => fn.apply(fn, arg));
};
return queuedFn;
function queuedFn() {
if (ready)
return fn.apply(fn, arguments);
queuedArgs.push(arguments);
}
}
var testerQueue = queue(tester)
function tester(x,y) {
console.log(x,y);
}
setTimeout(function() {
testerQueue.ready();
}, 3000);
testerQueue(1,2);
testerQueue(3,4);
testerQueue(5,6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment