Hyperlinkv0.8.0-beta.28

UndefinedOr

UndefinedOr.liftThrowableconsteffect/UndefinedOr.ts:139
<A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (
  ...a: A
) => B | undefined

Converts a throwing function into one that returns successful results unchanged and returns undefined when the function throws.

When to use

Use to adapt exception-throwing functions when undefined is the absence value you want to return for failures.

Gotchas

Thrown values are discarded. If the wrapped function can successfully return undefined, that success is indistinguishable from a thrown failure.

converting
export const liftThrowable = <A extends ReadonlyArray<unknown>, B>(
  f: (...a: A) => B
): (...a: A) => B | undefined =>
(...a) => {
  try {
    return f(...a)
  } catch {
    return undefined
  }
}