0.1.1-alpha.0 • Published 3 years ago

@talesoft/frame-loop v0.1.1-alpha.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

@talesoft/try-catch

A simple functional wrapper for a try-catch statement.

Makes it easier to handle exceptions immutably.

Usage

import tryCatch from '@talesoft/try-catch'

function somethingThatCanThrow() {

  if (amIWrong()) {

    throw new Error('I\'m wrong!')
  }

  return 'Some result'
}

const result = tryCatch(somethingThatCanThrow)

// result is of type `string | Error`

const alwaysResult = tryCatch(somethingThatCanThrow, () => 'Other result')

// alwaysResult is of type `string` ('Some result' or 'Other result')

const alwaysResult = tryCatch(somethingThatCanThrow, error => error.message)

// alwaysResult is of type `string` ('Some result' or 'I\'m wrong!')