@7i7o/git-remote-proland
Version:
Git Remote Helper
24 lines (22 loc) • 548 B
text/typescript
type WithAsyncFn<T = unknown> = () => T | Promise<T>
type WithAsyncReturn<TData, TError> = Promise<{
response: TData | null
error: TError | unknown
}>
export async function withAsync<TData = unknown, TError = unknown>(
fn: WithAsyncFn<TData>
): WithAsyncReturn<TData, TError> {
try {
if (typeof fn !== 'function') throw new Error('The first argument must be a function')
const response = await fn()
return {
response,
error: null
}
} catch (error) {
return {
error,
response: null
}
}
}