Creates a failed Lazy that will throw when evaluated
Creates a Lazy from an Either
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.get() // Prints "Computing..." and returns 42
const cached = expensive.get() // Returns 42 without printing
// Error handling
const risky = Lazy(() => {
if (Math.random() > 0.5) throw new Error("Failed")
return "Success"
})
const safe = risky.getOrElse("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.