• Wraps a promise and returns a tuple with either an error or the result. Optionally executes a finally callback after the promise settles.

    Example


    import { to } from "@mrspartak/promises"
    import { api } from "./api"

    // Simple tuple destructuring
    const [apiError, user] = await to(api.get("/me"))
    if (apiError) {
    // Handle error
    }

    // Using finally
    $component.isLoading = true
    const [apiError, status] = await to(api.post("/me/status", { status: "online" }), () => {
    $component.isLoading = false
    })
    if (apiError) {
    // Handle error
    }

    Type Parameters

    • $Promise

      The type of the value the input promise resolves to.

    Parameters

    • promise: ToIn<$Promise>

      The input promise to be wrapped.

    • Optional _finally: ToFinally

      Optional finally callback to be executed after the promise settles.

    Returns Promise<ToOut<$Promise>>

    A promise that resolves to a tuple containing either an error or the result.