Function: try()

Executes a function that may throw and wraps the result in a Result or ResultAsync.

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 { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<number, Error>
result
=
import Result
Result
.
try<() => number, Error>(options: {
    try: () => number;
    catch: (error: unknown) => Error;
}): Result.Result<number, Error> (+3 overloads)
export try

Executes a function that may throw and wraps the result in a Result or ResultAsync .

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

@function@typeParamT - The function type to execute (sync or async) or a Promise type.@typeParamE - The error type to return if catch is used.@returnsA Result or ResultAsync wrapping the function's return value or the caught error.@example

Sync try-catch

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

const result = Result.try({
try: () => {
const x = Math.random() * 10 - 5;
if (x < 0) throw new Error('Negative!');
return x * 2;
},
catch: (error) => new Error('Oops!', { cause: error }),
});

// result is Result<number, Error>
@example

Sync safe

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

const result = Result.try({
safe: true,
try: () => Math.random() + 1,
});

// result is Result<number, never>
@example

Async try-catch

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>
@example

Async safe

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

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

// result is ResultAsync<string, never>
@categoryCreators
try
({
try: () => number
try
: () => {
const
const x: number
x
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() * 10 - 5;
if (
const x: number
x
< 0) throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Negative!');
return
const x: number
x
* 2;
},
catch: (error: unknown) => Error
catch
: (
error: unknown
error
) => new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Oops!', {
ErrorOptions.cause?: unknown
cause
:
error: unknown
error
}),
}); // result is Result<number, Error>
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<number, never>
result
=
import Result
Result
.
try<() => number>(options: {
    safe: true;
    try: () => number;
}): Result.Result<number, never> (+3 overloads)
export try

Executes a function that may throw and wraps the result in a Result or ResultAsync .

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

@function@typeParamT - The function type to execute (sync or async) or a Promise type.@typeParamE - The error type to return if catch is used.@returnsA Result or ResultAsync wrapping the function's return value or the caught error.@example

Sync try-catch

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

const result = Result.try({
try: () => {
const x = Math.random() * 10 - 5;
if (x < 0) throw new Error('Negative!');
return x * 2;
},
catch: (error) => new Error('Oops!', { cause: error }),
});

// result is Result<number, Error>
@example

Sync safe

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

const result = Result.try({
safe: true,
try: () => Math.random() + 1,
});

// result is Result<number, never>
@example

Async try-catch

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>
@example

Async safe

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

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

// result is ResultAsync<string, never>
@categoryCreators
try
({
safe: true
safe
: true,
try: () => number
try
: () =>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() + 1,
}); // result is Result<number, never>
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.ResultAsync<Response, Error>
result
=
import Result
Result
.
try<() => Promise<Response>, Error>(options: {
    try: () => Promise<Response>;
    catch: (error: unknown) => Error;
}): Result.ResultAsync<Response, Error> (+3 overloads)
export try

Executes a function that may throw and wraps the result in a Result or ResultAsync .

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

@function@typeParamT - The function type to execute (sync or async) or a Promise type.@typeParamE - The error type to return if catch is used.@returnsA Result or ResultAsync wrapping the function's return value or the caught error.@example

Sync try-catch

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

const result = Result.try({
try: () => {
const x = Math.random() * 10 - 5;
if (x < 0) throw new Error('Negative!');
return x * 2;
},
catch: (error) => new Error('Oops!', { cause: error }),
});

// result is Result<number, Error>
@example

Sync safe

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

const result = Result.try({
safe: true,
try: () => Math.random() + 1,
});

// result is Result<number, never>
@example

Async try-catch

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>
@example

Async safe

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

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

// result is ResultAsync<string, never>
@categoryCreators
try
({
try: () => Promise<Response>
try
: () =>
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
('/api/data'),
catch: (error: unknown) => Error
catch
: (
error: unknown
error
) => new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Fetch failed', {
ErrorOptions.cause?: unknown
cause
:
error: unknown
error
}),
}); // result is ResultAsync<Response, Error>
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.ResultAsync<string, never>
result
=
import Result
Result
.
try<() => Promise<string>>(options: {
    safe: true;
    try: () => Promise<string>;
}): Result.ResultAsync<string, never> (+3 overloads)
export try

Executes a function that may throw and wraps the result in a Result or ResultAsync .

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

@function@typeParamT - The function type to execute (sync or async) or a Promise type.@typeParamE - The error type to return if catch is used.@returnsA Result or ResultAsync wrapping the function's return value or the caught error.@example

Sync try-catch

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

const result = Result.try({
try: () => {
const x = Math.random() * 10 - 5;
if (x < 0) throw new Error('Negative!');
return x * 2;
},
catch: (error) => new Error('Oops!', { cause: error }),
});

// result is Result<number, Error>
@example

Sync safe

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

const result = Result.try({
safe: true,
try: () => Math.random() + 1,
});

// result is Result<number, never>
@example

Async try-catch

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>
@example

Async safe

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

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

// result is ResultAsync<string, never>
@categoryCreators
try
({
safe: true
safe
: true,
try: () => Promise<string>
try
: () =>
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.resolve<string>(value: string): Promise<string> (+2 overloads)

Creates a new resolved promise for the provided value.

@paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
resolve
('ok'),
}); // result is ResultAsync<string, never>

Call Signature

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

Type Parameters

T

T extends () => Promise<any>

E

E

Parameters

options

catch

(error) => E

try

T

Returns

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

Call Signature

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

Type Parameters

T

T extends () => Promise<any>

Parameters

options

safe

true

try

T

Returns

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

Call Signature

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

Type Parameters

T

T extends () => any

E

E

Parameters

options

catch

(error) => E

try

T

Returns

Result<ReturnType<T>, E>

Call Signature

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

Type Parameters

T

T extends () => any

Parameters

options

safe

true

try

T

Returns

Result<ReturnType<T>, never>