@piyawasin/attempt
Version:
A minimal, type-safe utility for defensive error handling in JS/TS. Inspired by Go, it uses tuple-based returns to eliminate try-catch nesting and handle synchronous/asynchronous errors predictably.
9 lines (8 loc) • 472 B
TypeScript
/**
* Executes an async function and returns a tuple [result, error].
* @param func Async function to execute
* @param args Arguments to pass to the function
*/
import type { AttemptResult } from "./types";
export declare function attemptAsync<T, E = unknown>(promise: Promise<T>): Promise<AttemptResult<T, E>>;
export declare function attemptAsync<T, Args extends any[], E = unknown>(func: (...args: Args) => Promise<T>, ...args: Args): Promise<AttemptResult<T, E>>;