trysafe
v0.0.3
Published
> Safe and simple error handling for TypeScript, inspired by Golang and Scala
Downloads
92
Maintainers
Readme
⛵ TrySafe
Safe and simple error handling for TypeScript, inspired by Golang and Scala
Install
yarn add trysafe
Usage
Try
and TryAsync
returns an Either
type defined in fp-ts.
import { Try, TryAsync } from 'trysafe'
// sync
const result = Try(() => {
if (condition) {
throw new Error('failed')
// or
throw 'failed'
}
return 'succeeded'
})
// async
const result = await TryAsync(async () => {
if (condition) {
throw new Error('failed')
// or
throw 'failed'
}
return 'succeeded'
})
// result: Either<Error, string>
if (result.isLeft()) {
// result.value: Error
console.error(result.value.message) // -> 'failed'
return
}
// result.value: string
console.log(result.value) // -> 'succeeded'