1.0.4 • Published 2 years ago
@hanakla/rescue v1.0.4
@hanakla/rescue
Type safe one line try-catch handler
Example
import { rescue } from '@hanakla/rescue'
const result = rescue(() => /* success or raise error process */)
if (result.error) { console.warn(result.error) }
if (result.result) { console.log(result.result) }
// Spread style
const [result, error] = rescue(() => /* ... */)
// Support async function
const [result, error] = await rescue(async () => /* async process */)
// Error type expection
class YourError extends Error {}
// when raises `YourError`, rescue handle and return result/
// Otherwise, rescue is not handle error it's rethrowing.
const result = rescue(() => /* process */, { expects: [YourError] })