Skip to content

Instantly share code, notes, and snippets.

@9oelM
Last active March 9, 2021 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9oelM/32479d991835abe36b76df719025bf49 to your computer and use it in GitHub Desktop.
Save 9oelM/32479d991835abe36b76df719025bf49 to your computer and use it in GitHub Desktop.
Try catch sync wrapper
export function tcSync<Fn extends (...args: any) => any, Deps extends Parameters<Fn> = Parameters<Fn>>(fn: Fn, deps: Deps): TcResult<ReturnType<Fn>> {
try {
const data: ReturnType<Fn> = fn(...deps);
return [null, data];
} catch (e) {
return [e] as [Error]
}
}
export type TcResult<Data> = [null, Data] | [Error];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment