/** * Represents a successful result where error is null and data is present */ export type Success = [null, T] /** * Represents a failure result where error contains an error instance and data is null */ export type Failure = [E, null] /** * Represents the result of an operation that can either succeed with T or fail with E */ export type Result = Success | Failure /** * Wraps a promise in a try-catch block and returns a tuple of [error, data] * where exactly one value is non-null * * @param promise The promise to execute safely * @returns A tuple of [error, data] where one is null */ export async function tryCatch(promise: Promise): Promise> { try { const data = await promise return [null, data] as Success } catch (error) { return [error as E, null] as Failure } }