Files
online-energieausweis/src/lib/tryCatch.ts
2025-03-25 19:15:16 -03:00

18 lines
411 B
TypeScript

// Types for the result object with discriminated union
type Success<T> = [T, null]
type Failure<E> = [null, E];
type Result<T, E = Error> = Success<T> | Failure<E>;
// Main wrapper function
export async function tryCatch<T, E = Error>(
promise: Promise<T>,
): Promise<Result<T, E>> {
try {
const data = await promise;
return [data, null];
} catch (error) {
return [null, error as E];
}
}