Created
March 14, 2018 21:52
-
-
Save tibawatanabe/cea1f81706301c09e2c7045ade65844b to your computer and use it in GitHub Desktop.
once in TS
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
// tslint:disable-next-line:ban-types | |
export function once(fn: Function): Function { | |
let executed = false; | |
let result: any; | |
// tslint:disable-next-line:only-arrow-functions | |
return function() { | |
if (executed) { | |
return result; | |
} else { | |
executed = true; | |
return result = fn.apply(null, arguments); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment