Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Created June 10, 2025 04:40
Show Gist options
  • Save xantiagoma/01876cc5c30969319a64afa1384e609a to your computer and use it in GitHub Desktop.
Save xantiagoma/01876cc5c30969319a64afa1384e609a to your computer and use it in GitHub Desktop.
type Result<T, E = Error> = [T, null] | [null, E];
export async function tryCatch<T, E = Error>(
promise: PromiseLike<T> | (() => PromiseLike<T>)
): Promise<Result<T, E>> {
try {
const data = await (typeof promise === 'function' ? promise() : promise);
return [data, null];
} catch (error) {
return [null, error as E];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment