Creates an already-evaluated Lazy. Used by fromJSON to reconstruct a
Lazy whose thunk was forced at serialize time — there is no original
thunk to defer because a closure cannot be JSON-serialized.
Functionally equivalent to Lazy.fromValue but reads with intent at
the call site.
Creates a failed Lazy that will throw when evaluated
Creates a Lazy from an Either
Reconstruct a Lazy from a JSON envelope emitted by serialize().toJSON()
or instance toJSON(). Verifies @functype === "Lazy". Success envelopes
become already-evaluated Lazies via Lazy.evaluated; failure envelopes
become Lazies whose forcing rethrows the deserialized Error (see
error-envelope.ts — instanceof SomeError does NOT survive but name
does).
Creates a Lazy from an Option
Creates a Lazy that will throw an error since promises need to be awaited first
Creates a Lazy from a Try
Creates a Lazy from an immediate value
Creates a Lazy from a thunk (deferred computation)
// Basic lazy evaluation
const expensive = Lazy(() => {
console.log("Computing...")
return 42
})
// Nothing printed yet
const result = expensive.orThrow() // Prints "Computing..." and returns 42
const cached = expensive.orThrow() // Returns 42 without printing
// Error handling
const risky = Lazy(() => {
if (Math.random() > 0.5) throw new Error("Failed")
return "Success"
})
const safe = risky.orElse("Default") // Returns "Success" or "Default"
const option = risky.toOption() // Some("Success") or None
const either = risky.toEither() // Right("Success") or Left(Error)
Creates a Lazy computation that defers evaluation until needed. Results are memoized after first evaluation.