Skip to content

guard

Make an async function return undefined if it rejects

144 bytes

Usage

The guard function allows you to make an async function return undefined if it rejects. This is useful when you want to handle errors in a functional way, such as returning a default value.

import * as _ from 'radashi'
const example = async () => {
throw new Error()
}
const result = (await _.guard(example)) ?? []
// []

Guard only specific errors

The 2nd argument to guard is an error predicate. If provided, the function will only return undefined if the error matches the predicate.

import * as _ from 'radashi'
const DEFAULT_USER = { name: 'John Doe' }
async function fetchUser(id: string) {
if (id === 'unknown') throw new Error('User does not exist')
if (id === 'oops') throw new ReferenceError()
return { name: 'Jim Jimmy' }
}
const isPlainError = (err: any) => err.name === 'Error'
const userA =
(await _.guard(() => fetchUser('unknown'), isPlainError)) ?? DEFAULT_USER
// { name: "John Doe"}
// This one will reject.
const userB = await _.guard(() => fetchUser('oops'), isPlainError).catch(e => e)
// [object ReferenceError]

Synchronous guards

The guard function also works with synchronous functions.

import * as _ from 'radashi'
function example() {
throw new Error()
}
const result = _.guard(example) ?? []
// []