Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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