Skip to content

Instantly share code, notes, and snippets.

@huozhi
Created January 12, 2025 16:58
Show Gist options
  • Save huozhi/f19c2dbde15c0729e65fd0eb48596700 to your computer and use it in GitHub Desktop.
Save huozhi/f19c2dbde15c0729e65fd0eb48596700 to your computer and use it in GitHub Desktop.
lazy load
// 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