Function: try()

Wraps a function execution (sync or async) or a Promise in a Result or ResultAsync type, capturing errors and returning them in a structured way.

You can use either a custom catch handler or rely on the safe: true option to assume the function cannot throw.

Type Param

The function type to execute (sync or async) or a Promise type.

Type Param

The error type to return if catch is used.

Examples

import { Result } from '@praha/byethrow';

const result = Result.try({
  try: fetch('/api/data'),
  catch: (error) => new Error('Fetch failed', { cause: error }),
});

// result is ResultAsync<Response, Error>
import { Result } from '@praha/byethrow';

const result = Result.try({
  safe: true,
  try: Promise.resolve('ok'),
});

// result is ResultAsync<string, never>
import { Result } from '@praha/byethrow';

const fn = Result.try({
  try: (x: number) => {
    if (x < 0) throw new Error('Negative!');
    return x * 2;
  },
  catch: (error) => new Error('Oops!', { cause: error }),
});

const result = fn(5); // Result.Result<number, Error>
import { Result } from '@praha/byethrow';

const fn = Result.try({
  safe: true,
  try: (x: number) => x + 1,
});

const result = fn(1); // Result.Result<number, never>
import { Result } from '@praha/byethrow';

const fn = Result.try({
  try: async (id: string) => await fetch(`/api/data/${id}`),
  catch: (error) => new Error('Oops!', { cause: error }),
});

const result = await fn('abc'); // Result.ResultAsync<Response, Error>
import { Result } from '@praha/byethrow';

const fn = Result.try({
  safe: true,
  try: async () => await Promise.resolve('ok'),
});

const result = await fn(); // Result.ResultAsync<string, never>

Call Signature

try<T, E>(options): ResultAsync<Awaited<T>, E>

Defined in: functions/try.ts:98

Type Parameters

T

T extends Promise<any>

E

E

Parameters

options

catch

(error) => E

try

T

Returns

ResultAsync<Awaited<T>, E>

Call Signature

try<T>(options): ResultAsync<Awaited<T>, never>

Defined in: functions/try.ts:101

Type Parameters

T

T extends Promise<any>

Parameters

options

safe

true

try

T

Returns

ResultAsync<Awaited<T>, never>

Call Signature

try<T, E>(options): (...args) => ResultAsync<Awaited<ReturnType<T>>, E>

Defined in: functions/try.ts:104

Type Parameters

T

T extends (...args) => Promise<any>

E

E

Parameters

options

catch

(error) => E

try

T

Returns

(...args): ResultAsync<Awaited<ReturnType<T>>, E>

Parameters

args

...Parameters<T>

Returns

ResultAsync<Awaited<ReturnType<T>>, E>

Call Signature

try<T>(options): (...args) => ResultAsync<Awaited<ReturnType<T>>, never>

Defined in: functions/try.ts:107

Type Parameters

T

T extends (...args) => Promise<any>

Parameters

options

safe

true

try

T

Returns

(...args): ResultAsync<Awaited<ReturnType<T>>, never>

Parameters

args

...Parameters<T>

Returns

ResultAsync<Awaited<ReturnType<T>>, never>

Call Signature

try<T, E>(options): (...args) => Result<ReturnType<T>, E>

Defined in: functions/try.ts:110

Type Parameters

T

T extends (...args) => any

E

E

Parameters

options

catch

(error) => E

try

T

Returns

(...args): Result<ReturnType<T>, E>

Parameters

args

...Parameters<T>

Returns

Result<ReturnType<T>, E>

Call Signature

try<T>(options): (...args) => Result<ReturnType<T>, never>

Defined in: functions/try.ts:113

Type Parameters

T

T extends (...args) => any

Parameters

options

safe

true

try

T

Returns

(...args): Result<ReturnType<T>, never>

Parameters

args

...Parameters<T>

Returns

Result<ReturnType<T>, never>