Skip to content

Instantly share code, notes, and snippets.

@worldoptimizer
Last active May 14, 2022 18:49
Show Gist options
  • Save worldoptimizer/f1b407e53d1db3d075c1b32eace5a1d7 to your computer and use it in GitHub Desktop.
Save worldoptimizer/f1b407e53d1db3d075c1b32eace5a1d7 to your computer and use it in GitHub Desktop.
function selfFullFilling(fn) {
let value;
return function() {
return value || (value = fn.apply(this, arguments));
};
}
const add = selfFullFilling(function(a, b) {
console.log("calculated!")
return a + b;
});
add(4,5) // returns 9 … logs "calculated"
add(4,5) // only returns 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment