Created
January 12, 2025 16:58
-
-
Save huozhi/f19c2dbde15c0729e65fd0eb48596700 to your computer and use it in GitHub Desktop.
lazy load
This file contains hidden or 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
// Lazy load a value and cache it from the `getter` | |
function lazy<T>(getter: () => T): { value: T } { | |
return { | |
get value() { | |
const evaluated = getter() | |
Object.defineProperty(this, 'value', { value: evaluated }) | |
return evaluated | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment