return-style
Version:
Non-intrusively convert the result of any function or promise to the user's desired style.
13 lines (11 loc) • 333 B
text/typescript
import { Awaitable } from '@blackglory/prelude'
export async function getResultErrorAsync<E = Error, T = unknown>(
fn: () => Awaitable<T>
): Promise<[result: T, error: undefined] | [result: undefined, error: E]> {
try {
const result = await fn()
return [result, void 0]
} catch (e: any) {
return [void 0, e]
}
}