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
import _
_
from 'radashi'
const
const example: () => Promise<never>
example
= async () => {
throw new
var Error: ErrorConstructor
new (message?: string) => Error
Error
()
}
const
const result: never[]
result
= (await
import _
_
.
guard<() => Promise<never>>(func: () => Promise<never>, shouldGuard?: (err: any) => boolean): Promise<undefined>
export guard

A helper to try an async function that returns undefined if it fails.

@seehttps://radashi.js.org/reference/async/guard

@example

const result = await guard(fetchUsers)() ?? [];

@version12.1.0

guard
(
const example: () => Promise<never>
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
import _
_
from 'radashi'
const
const DEFAULT_USER: {
name: string;
}
DEFAULT_USER
= {
name: string
name
: 'John Doe' }
async function
function fetchUser(id: string): Promise<{
name: string;
}>
fetchUser
(
id: string
id
: string) {
if (
id: string
id
=== 'unknown') throw new
var Error: ErrorConstructor
new (message?: string) => Error
Error
('User does not exist')
if (
id: string
id
=== 'oops') throw new
var ReferenceError: ReferenceErrorConstructor
new (message?: string) => ReferenceError (+1 overload)
ReferenceError
()
return {
name: string
name
: 'Jim Jimmy' }
}
const
const isPlainError: (err: any) => boolean
isPlainError
= (
err: any
err
: any) =>
err: any
err
.
any
name
=== 'Error'
const
const userA: {
name: string;
}
userA
=
(await
import _
_
.
guard<() => Promise<{
name: string;
}>>(func: () => Promise<{
name: string;
}>, shouldGuard?: (err: any) => boolean): Promise<{
name: string;
} | undefined>
export guard

A helper to try an async function that returns undefined if it fails.

@seehttps://radashi.js.org/reference/async/guard

@example

const result = await guard(fetchUsers)() ?? [];

@version12.1.0

guard
(() =>
function fetchUser(id: string): Promise<{
name: string;
}>
fetchUser
('unknown'),
const isPlainError: (err: any) => boolean
isPlainError
)) ??
const DEFAULT_USER: {
name: string;
}
DEFAULT_USER
// { name: "John Doe"}
// This one will reject.
const
const userB: any
userB
= await
import _
_
.
guard<() => Promise<{
name: string;
}>>(func: () => Promise<{
name: string;
}>, shouldGuard?: (err: any) => boolean): Promise<{
name: string;
} | undefined>
export guard

A helper to try an async function that returns undefined if it fails.

@seehttps://radashi.js.org/reference/async/guard

@example

const result = await guard(fetchUsers)() ?? [];

@version12.1.0

guard
(() =>
function fetchUser(id: string): Promise<{
name: string;
}>
fetchUser
('oops'),
const isPlainError: (err: any) => boolean
isPlainError
).
Promise<{ name: string; } | undefined>.catch<any>(onrejected?: ((reason: any) => any) | null | undefined): Promise<any>

Attaches a callback for only the rejection of the Promise.

@paramonrejected The callback to execute when the Promise is rejected.

@returnsA Promise for the completion of the callback.

catch
(
e: any
e
=>
e: any
e
)
// [object ReferenceError]

Synchronous guards

The guard function also works with synchronous functions.

import * as
import _
_
from 'radashi'
function
function example(): void
example
() {
throw new
var Error: ErrorConstructor
new (message?: string) => Error
Error
()
}
const
const result: never[]
result
=
import _
_
.
guard<() => void>(func: () => void, shouldGuard?: (err: any) => boolean): void | undefined
export guard

A helper to try an async function that returns undefined if it fails.

@seehttps://radashi.js.org/reference/async/guard

@example

const result = await guard(fetchUsers)() ?? [];

@version12.1.0

guard
(
function example(): void
example
) ?? []
// []