Skip to content

Instantly share code, notes, and snippets.

@exbotanical
Created July 8, 2020 00:02
Show Gist options
  • Save exbotanical/73da0588595879b4b395672ba6a86282 to your computer and use it in GitHub Desktop.
Save exbotanical/73da0588595879b4b395672ba6a86282 to your computer and use it in GitHub Desktop.
/* Snippets: Higher-order Functions and Functional Programming Patterns */
/* Execute function at N interval */
const setNIntervals = (fn, delay, rounds) => {
if (!rounds) {
return;
}
setTimeout(() => {
fn();
setNIntervals(fn, delay, rounds - 1);
}, delay);
};
// will fire 3 times, once every second
const mockHandler = (...args) => setNIntervals(() => console.log(...args), 1000, 3);
/* Call variable num of functions, else log all args */
const logOrFire = (...args) => {
for (let i = 0; i <= args.length - 1; i++) {
if (typeof args[i] === "function") {
args[i]();
}
else {
return console.log(...args);
}
}
};
logOrFire("[-]", "[=]");
// [-] [=]
const x = () => console.log("[*]");
const y = () => console.log("[+]");
logOrFire(x, y);
// [*]
// [+]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment