Skip to main content

@rtorcato/js-common / try

try

Type Aliases

Success

Success<T> = object

Defined in: try/trycatch.ts:4

Successful branch of a Result — carries the value and a null error.

Type Parameters

T

T

Properties

data

data: T

Defined in: try/trycatch.ts:4

error

error: null

Defined in: try/trycatch.ts:4


Failure

Failure<E> = object

Defined in: try/trycatch.ts:9

Error branch of a Result — carries a null value and an error of type E.

Type Parameters

E

E

Properties

data

data: null

Defined in: try/trycatch.ts:9

error

error: E

Defined in: try/trycatch.ts:9


Result

Result<T, E> = Success<T> | Failure<E>

Defined in: try/trycatch.ts:15

Go-style discriminated union representing either a Success<T> or a Failure<E>, for async code that prefers explicit error returns over thrown exceptions.

Type Parameters

T

T

E

E = Error

Functions

isSuccess()

isSuccess<T, E>(result): result is Success<T>

Defined in: try/trycatch.ts:20

Type guard that narrows a Result to its Success branch when the error is null.

Type Parameters

T

T

E

E

Parameters

result

Result<T, E>

Returns

result is Success<T>


tryCatch()

tryCatch<T, E>(fn): Promise<Result<T, E>>

Defined in: try/trycatch.ts:28

Run an async function and capture any thrown error into a Result, eliminating try/catch at the call site.

Type Parameters

T

T

E

E = Error

Parameters

fn

() => Promise<T>

Returns

Promise<Result<T, E>>