Created
August 3, 2022 10:55
-
-
Save sebinsua/5deeec116caf883f101646f039e9bc2d 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
function maxCall1(fn, n) { | |
let count = 0; | |
return function withMaxCall(...args) { | |
if (count + 1 > n) { | |
return; | |
} | |
count++; | |
return fn(...args); | |
}; | |
} | |
function maxCall2(fn, n) { | |
function withMaxCall(...args) { | |
withMaxCall.callCount++; | |
if (withMaxCall.callCount > n) { | |
return; | |
} | |
return fn(...args); | |
} | |
withMaxCall.callCount = 0; | |
return withMaxCall; | |
} | |
function helloWorld() { | |
console.log("hello world!"); | |
} | |
const fn = maxCall2(helloWorld, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment