UNPKG

catchbuddy

Version:

Simplify error handling in JavaScript with a lightweight utility that organizes results and errors for both synchronous and asynchronous operations.

26 lines (24 loc) 1.43 kB
type Result<TResult, TError = Error> = [TError, undefined] | [undefined, TResult]; /** * Resolves a given asynchronous function, returning a result in a tuple format. * * @template T * @param {Promise<T>} promise - The asynchronous function to resolve. * @returns {Promise<Result<T>>} A promise that resolves to a tuple where the first element is an error object (if the promise was rejected) or `undefined`, and the second element is the data (if resolved successfully) or `undefined`. */ declare function resolve<T>(promise: Promise<T>): Promise<Result<T>>; /** * Executes a provided function with optional arguments and returns its result or any error thrown. * * @template T * @template Args * @param {(...args: Args) => T} fn - The function to execute. * @param {...Args} args - The arguments to pass to the function. * @return {Result<T>} A tuple containing any error encountered and the result of the function execution. * If the function executes successfully, the first element is `undefined` and * the second element is the result. * If the function throws an error, the first element is the error and * the second element is `undefined`. */ declare const resolveSync: <T = unknown, Args extends unknown[] = never[]>(fn: (...args: Args) => T, ...args: Args) => Result<T>; export { type Result, resolve, resolveSync };