Skip to content

Instantly share code, notes, and snippets.

@cafedomingo
Last active March 26, 2026 00:35
Show Gist options
  • Select an option

  • Save cafedomingo/629083 to your computer and use it in GitHub Desktop.

Select an option

Save cafedomingo/629083 to your computer and use it in GitHub Desktop.
Takes a function and returns a function that will only be run once.
const runOnce = function(func) {
let hasRun = false;
let result;
return function(...args) {
if (!hasRun) {
hasRun = true;
result = func.apply(this, args);
}
return result;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment