▸ make<F
>(catchable
): F
extends AnyAsyncFn
? (…args
: Parameters
<F
>) => Promise
<Awaited
<ReturnType
<F
>>> : (…args
: Parameters
<F
>) => ReturnType
<F
>
Uses the Functors.Catchable<f>
to return a wrapped function f
.
If the function f
throws internally, then the functors’ catch function is called, otherwise returns f
output
Example
import { Catch, Functors, Num } from 'tiinvo'
function catchy(arg: number) {
if (Num.isEven(arg)) {
throw new TypeError("I expected an odd number :(")
}
return arg;
}
const c: Functors.CatchableModule<typeof catchy> = {
[Functors.catchableSync]() {
return {
catch: (_error, args) => args[0] - 1,
func: catchy,
}
}
}
const catched = Catch.make(c);
catched(10) // 9
catched(7) // 7
Since
4.0.0
Name | Type | Description |
---|---|---|
F |
extends AnyFn |
the function to catch |
Name | Type | Description |
---|---|---|
catchable |
CatchableModule <F > |
the CatchableModule |
F
extends AnyAsyncFn
? (…args
: Parameters
<F
>) => Promise
<Awaited
<ReturnType
<F
>>> : (…args
: Parameters
<F
>) => ReturnType
<F
>
a unary function