safeCallback
Runs a callback in a safe block. If the callback throws or returns a rejected promise, the function returns or resolves to a "safe-callback-error" typed Left instead of propagating the error. If the callback returns an Either, it is kept as-is. Promise results are handled after resolution.
Interactive example
Syntax
typescript
function safeCallback<
const GenericOutput extends unknown
>(
theFunction: () => GenericOutput
): Extract<ComputeSafeCallbackResult<GenericOutput>, any>;Parameters
theFunction: Callback to execute in a safe block. It can return a direct value, anEither, or a promise of either.
Return value
- If the callback returns a
LeftorRight: theEitheris returned as-is. - If the callback succeeds with a non-
Eithervalue: the value is wrapped inSafeCallbackSuccess. - If the callback returns a promise: the resolved value follows the same rules.
- If the callback throws or the promise rejects:
SafeCallbackError(left("safe-callback-error", error)) is returned or resolved.
See also
left– Build a typedLeft.whenHasInformation– Pattern match on"safe-callback-error".
