Skip to content

Instantly share code, notes, and snippets.

@ahamed
Created July 19, 2022 17:13
Show Gist options
  • Save ahamed/84231837f4e979f758cc7ff64decbee7 to your computer and use it in GitHub Desktop.
Save ahamed/84231837f4e979f758cc7ff64decbee7 to your computer and use it in GitHub Desktop.
Extract [response, error] from a promise function.
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