trysafely
Version:
A robust async helper to wrap promises and functions, returning [result, error] for graceful error handling.
43 lines (40 loc) • 1.11 kB
TypeScript
/**
- Attempts to run an **async** function safely.
- @template T
- @param fn - A function returning a Promise.
- @returns An object `{ data, err }` where `data` is the resolved value or `null`, and `err` is the caught error or `null`.
*/
export declare function tryAsync<T>(fn: () => Promise<T>): Promise<{
data: Awaited<T>;
err: null;
} | {
data: null;
err: Error;
}>;
/**
- Attempts to run a **sync** function safely.
- @template T
- @param fn - A function returning a synchronous result.
- @returns An object `{ data, err }`
*/
export declare function trySync<T>(fn: () => T): {
data: T;
err: null;
} | {
data: null;
err: Error;
};
/**
- Attempts to run a function safely, whether it returns a value or a promise.
- @template T - The type of the function's return value.
- @param fn - A function that returns either T or Promise.
- @returns A Promise resolving to `{ data, err }`.
*/
export default function trySafely<T>(fn: () => T | Promise<T>): Promise<{
data: T;
err: null;
} | {
data: null;
err: Error;
}>;
//# sourceMappingURL=index.d.ts.map