export enum LazyStatus { Pending, Resolved, Error, } export class Lazy { #value?: T; #resolver: () => Promise; status: LazyStatus; constructor(resolver: () => Promise) { this.#resolver = resolver; this.status = LazyStatus.Pending; } async value(): Promise { if (!this.#value) { try { this.#value = await this.#resolver(); } catch (error) { this.status = LazyStatus.Error; console.error(error); return null; } } this.status = LazyStatus.Resolved; return this.#value; } }