Skip to content

safeCallback

Runs a callback in a safe block. If the callback throws, the function returns a "callback" typed EitherLeft instead of propagating the exception.

Interactive example

Syntax

typescript
function safeCallback<GenericOutput extends unknown>(
  theFunction: () => GenericOutput
): GenericOutput | EitherCallbackError;

Parameters

  • theFunction : Callback to execute in a safe block.

Return value

  • If the callback succeeds: the value returned by theFunction.
  • If the callback throws: EitherCallbackError (alias of left("callback", error)).

See also

Released under the MIT license.