Skip to content

Instantly share code, notes, and snippets.

@tibawatanabe
Created March 14, 2018 21:52
Show Gist options
  • Save tibawatanabe/cea1f81706301c09e2c7045ade65844b to your computer and use it in GitHub Desktop.
Save tibawatanabe/cea1f81706301c09e2c7045ade65844b to your computer and use it in GitHub Desktop.
once in TS
// 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