Function ado

"Async-do" -- an alias for the doingAsync function.

This alias exists to provide a shorter, more concise name for the doingAsync function.

doingAsync

  • Execute an async 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> | Promise<T | Result<T, E>>) => Promise<T>)) => Promise<T>)

      The asynchronous 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): Promise<T>
        • Parameters

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

                If the provided value is a promised value, it will first be resolved. If the resolved 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 Promise<T>

                The value of the Ok result.

                • doingAsync
                • bindResult

          Returns Promise<T>

    • catchall: boolean = true

    Returns Promise<Result<T, E>>

    The promised 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 = doingAsync(async () => {
    const response = await fetch("https://example.com/data");
    const json = await response.json();
    return obj;
    });

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

    // ...

    const invalidResult = doingAsync(async () => {
    const response = await fetch("https://example.com/data"); // throws connection error
    const json = await response.json();
    return obj;
    });

    console.log(invalidResult);
    // => Err(TypeError: Failed to fetch)
        async function safeFetch<T>(url: string): Result<Response, Error> {
    return doing(() => fetch(url));
    }

    const result = ado(async bind => {
    const url = "https://example.com/data";
    const res = await bind(safeFetch(url));
    const data = await res.json();
    return data["name"];
    });

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