Created
February 3, 2016 15:25
-
-
Save tdantas/24840d1e6f6c4c9c2750 to your computer and use it in GitHub Desktop.
delaying the execution of your function until ready be called
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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