Created
July 8, 2020 00:02
-
-
Save exbotanical/73da0588595879b4b395672ba6a86282 to your computer and use it in GitHub Desktop.
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
/* 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