Types
tc
is written in TypeScript, with fully automatic type checking.
Parameters
function tc<T = unknown, U = T>(
t: () => MaybePromise<T>, // try
c?: (e: unknown) => MaybePromise<U> // catch
): MaybePromise<[T, undefined] | [U | undefined, unknown]>
Return values
The unwrapped (awaited) return value of tc
will always be an array of two items:
T
(ift
succeeds),U
(ifc
succeeds), orundefined
undefined
(ift
succeeds) orunknown
- If
t
andc
both fail, the error will be fromc
- If
Overload signatures
tc
will determine its return type based on the return types of your t
and c
callbacks. You shouldn't need to pass in type arguments for accurate type checking.
function tc<T = unknown, U = T>(
t: () => T,
c?: (e: unknown) => U
): [T, undefined] | [U | undefined, unknown]
function tc<T = unknown, U = T>(
t: () => T,
c?: (e: unknown) => Promise<U>
): [T, undefined] | Promise<[U | undefined, unknown]>
function tc<T = unknown, U = T>(
t: () => Promise<T>,
c?: (e: unknown) => MaybePromise<U>
): Promise<[T, undefined] | [U | undefined, unknown]>