Created
July 19, 2022 17:13
-
-
Save ahamed/84231837f4e979f758cc7ff64decbee7 to your computer and use it in GitHub Desktop.
Extract [response, error] from a promise function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const extractPromise = async < | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
F extends (...args: any) => Promise<any>, | |
A extends Parameters<F>, | |
R extends ReturnType<F>, | |
>( | |
promiseFn: F, | |
...args: A | |
): Promise<[Awaited<R> | undefined, unknown | undefined]> => { | |
try { | |
const response = await promiseFn.call(null, ...args); | |
return [response, undefined]; | |
} catch (error) { | |
return [undefined, error]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment