Skip to content

Safe -> Success | Fail

function Fusion.Safe<Success, Fail>(
    callbacks: {
        try: () -> Success,
        fallback: (err: unknown) -> Fail
    }
): Success | Fail

Safely runs a function and returns the value it produces. If the function fails, the fallback function can handle the error and produces a fallback value.

Safe acts like a version of xpcall that is easier to use in calculations and expressions, because it only returns the values from the functions, rather than returning a success boolean.

Fatal versus non-fatal errors

Safe only protects you from errors that would stop your calculation from successfully returning a value.

In particular, this applies to computeds you create inside Safe (and other similar objects). Because errors there are safely handled by those objects, and do not cause the Safe calculation to crash fatally, you have to use Safe inside of the computed itself if you want to capture the error.


Properties

try : () -> Success

The possibly erroneous calculation or expression.

fallback : (err: unknown) -> Fail

A fallback calculation that should provide a backup answer if the possibly erroneous calculation throws an error.


Returns -> Success | Fail

The value produced by try if it's successful, or the value produced by fallback if an error occurs during try.

Back to top