asyncSafeCallback
Runs a callback or a promise in a safe asynchronous block. If the callback throws or the promise rejects, the function resolves to a "safe-callback-error" typed Left instead of propagating the error. If the input produces an Either, it is kept as-is. Other resolved values are wrapped in a Right.
Interactive example
Syntax
typescript
function asyncSafeCallback<
const GenericOutput extends unknown
>(
maybeFunction: (() => GenericOutput) | Promise<GenericOutput>
): Promise<Extract<ComputeSafeCallbackResult<GenericOutput>, any>>;Parameters
maybeFunction: Callback or promise to execute safely. The result can be a direct value, anEither, or a promise of either.
Return value
A promise that resolves with these rules:
- If the input produces a
LeftorRight: theEitheris returned as-is. - If the input succeeds with a non-
Eithervalue: the value is wrapped inSafeCallbackSuccess. - If the callback throws or the promise rejects:
SafeCallbackError(left("safe-callback-error", error)) is resolved.
See also
safeCallback– Version that keeps a synchronous output when possible.left– Build a typedLeft.
