Function _do

Alias for doing function.

This alias exists to provide a shorter, more concise name for the doing function. It starts with an underscore to avoid conflicts with the do keyword in JavaScript.

doing

  • Execute a block of code binding results to local variables using the bind function, propagating side effects to the caller.

    This function is a core feature to using the Result jonad. It should be used to wrap any function implementation, allowing results returned from other functions to have the value returned to continue execution, while capturing any errors that are returned or thrown and propagating them to the caller as a new Result.

    This provides additional safety when implementing core business logic, as it ensures that any errors that occur are intentionally handled up the call chain, intead of exceptions propagating up and causing the program to exit unexpectedly. It also allows you, the developer, to focus solely on the happy path of your code without worries of handling potential exceptions.

    Type Parameters

    • T
    • E extends Error

    Parameters

    • block: ((bind: (<T, E>(result: T | Result<T, E>) => T)) => T)

      The block of code to execute. The block accepts a function bind as an argument that can be used to unwrap a Result by returning the value if it's an Ok, or propagating the error if it's an Err.

        • (bind): T
        • Parameters

          • bind: (<T, E>(result: T | Result<T, E>) => T)
              • <T, E>(result): T
              • Bind a value to a variable.

                If the value is a result, it will be bound if it is an Ok, otherwise the error will be propagated to the caller of the doing/doingAsync function and returned as a Result containing an error. If the value is not a result, it is returned as is.

                Type Parameters

                • T
                • E extends Error

                Parameters

                Returns T

                The value of the Ok result.

                If the result is an Err.

                doing

          Returns T

    • catchall: boolean = true

    Returns Result<T, E>

    The result of the block. If the block reaches the end, the returned value will be an Ok containing the value returned by the block. If the block fails, the result will be an Err containing the error that caused the failure.

        const result = doing(() => {
    const jsonStr = '{"name": "Alice"}';
    const obj = JSON.parse(jsonStr);
    return obj;
    });

    console.log(result);
    // => Ok({ name: "Alice" })

    // ...

    const invalidResult = doing(() => {
    const jsonStr = '{"name": "Alice"';
    const obj = JSON.parse(jsonStr); // throws SyntaxError
    return obj;
    });

    console.log(invalidResult);
    // => Err(SyntaxError: Unexpected end of JSON input)
        function safeParseJson<T>(jsonStr: string): Result<T, Error> {
    return doing(() => JSON.parse(jsonStr));
    }

    const result = _do(bind => {
    const jsonStr = '{"name": "Alice"}';
    const obj = bind(safeParseJson<{ name: string }>(jsonStr));
    return obj["name"];
    });

    console.log(result);
    // => Ok("Alice")