Lazy
Esta página aún no está disponible en tu idioma.
Defined in: src/util/Lazy.ts:33
Type Parameters
Section titled “Type Parameters”T
Accessors
Section titled “Accessors”isEvaluated
Section titled “isEvaluated”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”flatMap()
Section titled “flatMap()”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.
Type Parameters
Section titled “Type Parameters”U
Parameters
Section titled “Parameters”(value) => Lazy<U>
Returns
Section titled “Returns”Lazy<U>
forEach()
Section titled “forEach()”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.
Parameters
Section titled “Parameters”(value) => void
Returns
Section titled “Returns”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.
Returns
Section titled “Returns”T
getSync()
Section titled “getSync()”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
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).
Type Parameters
Section titled “Type Parameters”U = T
Returns
Section titled “Returns”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.
Type Parameters
Section titled “Type Parameters”U
Parameters
Section titled “Parameters”(value) => U
Returns
Section titled “Returns”Lazy<U>
peek()
Section titled “peek()”peek():
T|undefined
Defined in: src/util/Lazy.ts:142
Return the cached value if already evaluated, else undefined.
Returns
Section titled “Returns”T | undefined
reset()
Section titled “reset()”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.
Returns
Section titled “Returns”void
setOverride()
Section titled “setOverride()”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.
Parameters
Section titled “Parameters”T | null
Returns
Section titled “Returns”void
evaluated()
Section titled “evaluated()”
staticevaluated<T>(value):Lazy<T>
Defined in: src/util/Lazy.ts:50
Build a lazy cell that’s already evaluated to value (no thunk runs).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”Lazy<T>
staticof<T>(compute):Lazy<T>
Defined in: src/util/Lazy.ts:47
Build a lazy cell from a thunk. Preferred entry point.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”compute
Section titled “compute”() => T
Returns
Section titled “Returns”Lazy<T>