Hyperlinkv0.8.0-beta.28

Option

Option.liftThrowableconsteffect/Option.ts:1042
<A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (
  ...a: A
) => Option<B>

Lifts a function that may throw into one that returns an Option.

When to use

Use to wrap exception-throwing APIs (e.g. JSON.parse) for safe usage

Details

  • If the function returns normally → Some with the result
  • If the function throws → None (exception is swallowed)

Example (Lifting JSON.parse)

import { Option } from "effect"

const parse = Option.liftThrowable(JSON.parse)

console.log(parse("1"))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }

console.log(parse(""))
// Output: { _id: 'Option', _tag: 'None' }
convertingliftNullishOr
Source effect/Option.ts:104210 lines
export const liftThrowable = <A extends ReadonlyArray<unknown>, B>(
  f: (...a: A) => B
): (...a: A) => Option<B> =>
(...a) => {
  try {
    return some(f(...a))
  } catch {
    return none()
  }
}
Referenced by 2 symbols