Created
February 25, 2018 03:46
-
-
Save mediaupstream/cf1b8c6c112008bd9bfc536b4c0964b7 to your computer and use it in GitHub Desktop.
repeat a function n number of times.
This file contains hidden or 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
| function repeat(n = 1) { | |
| let i = 0; | |
| return fn => { | |
| while(n--) { | |
| i++; | |
| fn(i) | |
| } | |
| } | |
| } | |
| // example usage: | |
| repeat(10)(i => { | |
| console.log('floop boop', i) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. I think this is on track, is there a way (is it even possible) to get rid of the anon function and instead just have curly braces as a direct function? leaving the i out of the mix entirely (at least what would be exposed to the end user of the function!?
so instead of
we'd have something like