Function: fn()

Wraps a function that may throw and returns a new function that returns 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 fn: (x: number) => Result.Result<number, Error>
fn
=
import Result
Result
.
fn<(x: number) => number, Error>(options: {
    try: (x: number) => number;
    catch: (error: unknown) => Error;
}): (x: number) => Result.Result<number, Error> (+3 overloads)
export fn

Wraps a function that may throw and returns a new function that returns 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 new function that returns a Result or ResultAsync wrapping the original function's return value or the caught error.@example

Sync try-catch

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

const fn = Result.fn({
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>
@example

Sync safe

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

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

const result = fn(1); // Result.Result<number, never>
@example

Async try-catch

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

const fn = Result.fn({
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>
@example

Async safe

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

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

const result = await fn(); // Result.ResultAsync<string, never>
@categoryCreators
fn
({
try: (x: number) => number
try
: (
x: number
x
: number) => {
if (
x: number
x
< 0) throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('Negative!');
return
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
}),
}); const
const result: Result.Result<number, Error>
result
=
const fn: (x: number) => Result.Result<number, Error>
fn
(5); // Result.Result<number, Error>
import { 
import Result
Result
} from '@praha/byethrow';
const
const fn: (x: number) => Result.Result<number, never>
fn
=
import Result
Result
.
fn<(x: number) => number>(options: {
    safe: true;
    try: (x: number) => number;
}): (x: number) => Result.Result<number, never> (+3 overloads)
export fn

Wraps a function that may throw and returns a new function that returns 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 new function that returns a Result or ResultAsync wrapping the original function's return value or the caught error.@example

Sync try-catch

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

const fn = Result.fn({
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>
@example

Sync safe

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

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

const result = fn(1); // Result.Result<number, never>
@example

Async try-catch

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

const fn = Result.fn({
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>
@example

Async safe

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

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

const result = await fn(); // Result.ResultAsync<string, never>
@categoryCreators
fn
({
safe: true
safe
: true,
try: (x: number) => number
try
: (
x: number
x
: number) =>
x: number
x
+ 1,
}); const
const result: Result.Result<number, never>
result
=
const fn: (x: number) => Result.Result<number, never>
fn
(1); // Result.Result<number, never>
import { 
import Result
Result
} from '@praha/byethrow';
const
const fn: (id: string) => Result.ResultAsync<Response, Error>
fn
=
import Result
Result
.
fn<(id: string) => Promise<Response>, Error>(options: {
    try: (id: string) => Promise<Response>;
    catch: (error: unknown) => Error;
}): (id: string) => Result.ResultAsync<Response, Error> (+3 overloads)
export fn

Wraps a function that may throw and returns a new function that returns 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 new function that returns a Result or ResultAsync wrapping the original function's return value or the caught error.@example

Sync try-catch

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

const fn = Result.fn({
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>
@example

Sync safe

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

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

const result = fn(1); // Result.Result<number, never>
@example

Async try-catch

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

const fn = Result.fn({
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>
@example

Async safe

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

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

const result = await fn(); // Result.ResultAsync<string, never>
@categoryCreators
fn
({
try: (id: string) => Promise<Response>
try
: async (
id: string
id
: string) => await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
(`/api/data/${
id: string
id
}`),
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
}),
}); const
const result: Result.Result<Response, Error>
result
= await
const fn: (id: string) => Result.ResultAsync<Response, Error>
fn
('abc'); // Result.ResultAsync<Response, Error>
import { 
import Result
Result
} from '@praha/byethrow';
const
const fn: () => Result.ResultAsync<string, never>
fn
=
import Result
Result
.
fn<() => Promise<string>>(options: {
    safe: true;
    try: () => Promise<string>;
}): () => Result.ResultAsync<string, never> (+3 overloads)
export fn

Wraps a function that may throw and returns a new function that returns 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 new function that returns a Result or ResultAsync wrapping the original function's return value or the caught error.@example

Sync try-catch

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

const fn = Result.fn({
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>
@example

Sync safe

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

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

const result = fn(1); // Result.Result<number, never>
@example

Async try-catch

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

const fn = Result.fn({
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>
@example

Async safe

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

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

const result = await fn(); // Result.ResultAsync<string, never>
@categoryCreators
fn
({
safe: true
safe
: true,
try: () => Promise<string>
try
: async () => await
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'),
}); const
const result: Result.Result<string, never>
result
= await
const fn: () => Result.ResultAsync<string, never>
fn
(); // Result.ResultAsync<string, never>

Call Signature

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

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

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

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

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

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

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

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>