UNPKG

return-style

Version:

Non-intrusively convert the result of any function or promise to the user's desired style.

13 lines (11 loc) 337 B
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 (err) { return [void 0, err as E] } }