Skip to content
English

Lazy

Defined in: src/util/Lazy.ts:33

T

get isEvaluated(): boolean

Defined in: src/util/Lazy.ts:137

true once .get() can return without running the thunk — either the thunk has already been executed (success or failure both count), or an override is active. false only in the initial pending state.

boolean

flatMap<U>(f): Lazy<U>

Defined in: src/util/Lazy.ts:159

Derive a new Lazy whose value flattens a Lazy<Lazy<U>>. Same laziness contract as map.

U

(value) => Lazy<U>

Lazy<U>


forEach(f): void

Defined in: src/util/Lazy.ts:167

Run a side effect against the evaluated value. Forces evaluation; idempotent by virtue of the cache.

(value) => void

void


get(): T

Defined in: src/util/Lazy.ts:61

Get the memoised value. Runs the thunk on first call; subsequent calls return the cached result. If the thunk threw, the same error is re-thrown on every subsequent access.

T


getSync<U>(): U

Defined in: src/util/Lazy.ts:110

Synchronous variant of get. Returns the cached value if already evaluated; throws otherwise.

For Lazy<Promise<U>> (i.e. async-initialised lazy), getSync returns the resolved value of type U once the underlying Promise has settled successfully — meaning callers no longer have to await after they know the lazy has been forced.

const sdk = Lazy.of(async () => await import(‘@aws-sdk/client-s3’)); await sdk.get(); // force + await once const mod = sdk.getSync(); // type-safe sync access after

Throws when:

  • the lazy has not been forced at all (get() was never called);
  • the lazy is forced but the underlying Promise hasn’t resolved yet;
  • the underlying Promise rejected (the rejection error is re-thrown);
  • the thunk itself threw (the original error is re-thrown).

The generic parameter U lets callers narrow the return type for the async case — Lazy<Promise<X>>.getSync<X>() returns X while Lazy<X>.getSync() still returns X (the cached non-Promise value).

U = T

U


map<U>(f): Lazy<U>

Defined in: src/util/Lazy.ts:151

Derive a new Lazy whose value is f(this.get()). The derivation is itself lazy — map(f) does not force the source.

U

(value) => U

Lazy<U>


peek(): T | undefined

Defined in: src/util/Lazy.ts:142

Return the cached value if already evaluated, else undefined.

T | undefined


reset(): void

Defined in: src/util/Lazy.ts:175

Forget the memoised value so the next get() re-runs the thunk. Primarily a test hook — also clears any override set via setOverride. In hot production paths, prefer building a new Lazy rather than resetting.

void


setOverride(value): void

Defined in: src/util/Lazy.ts:186

Test hook: force .get() to return value without touching the thunk or the cache. Call reset() (or setOverride(null)) to restore normal evaluation.

T | null

void


static evaluated<T>(value): Lazy<T>

Defined in: src/util/Lazy.ts:50

Build a lazy cell that’s already evaluated to value (no thunk runs).

T

T

Lazy<T>


static of<T>(compute): Lazy<T>

Defined in: src/util/Lazy.ts:47

Build a lazy cell from a thunk. Preferred entry point.

T

() => T

Lazy<T>