Improving Code Snippets

Sometimes even the smallest thing stumps me. It there no better way of writing this?

const getNumber = (number: Effect.Effect<Option.Option<number>>) =>
  number.pipe(
    Effect.flatMap(identity),
    Effect.catchTag('NoSuchElementException', () => new NoNumberProvided()),
  )

😢

Or like this

const getNumber2 = (number: Effect.Effect<Option.Option<number>>) =>
  Effect.flatMap(number, (a) =>
    Option.isSome(a) ? Effect.succeed(a.value) : Effect.fail(new NoNumberProvided()),
  )

😭

I guess I missing the obvious solution. I would like Effect.getOrFail(new NoNumberProvided()) but because is does not exists, there must be an easy solution... Anyone?
Was this page helpful?