A helper to try an async function that returns undefined if it fails.
const result = (await guard(fetchUsers)()) ?? [];
npx atmx add helper guard
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** A helper to try an async function that returns undefined if it* fails.** @example* const result = await guard(fetchUsers)() ?? [];*/export function guard<TFunction extends () => any>(func: TFunction,shouldGuard?: (err: any) => boolean,): ReturnType<TFunction> extends Promise<any>? Promise<Awaited<ReturnType<TFunction>> | undefined>: ReturnType<TFunction> | undefined {const _guard = (err: any) => {if (shouldGuard && !shouldGuard(err)) { throw err;}return undefined as any;};try {const result = func();return result instanceof Promise ? result.catch(_guard) : result;} catch (err) {return _guard(err);}}