Skip to content

Instantly share code, notes, and snippets.

@degitgitagitya
Created July 27, 2021 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save degitgitagitya/920b653e7b8db0d64e743d13ccf34dd3 to your computer and use it in GitHub Desktop.
Save degitgitagitya/920b653e7b8db0d64e743d13ccf34dd3 to your computer and use it in GitHub Desktop.
Clean Promise Handler
/**
* @param {Promise<T>} promise
*/
export const resolvePromise = async <T>(promise: Promise<T>) => {
try {
const data = await promise;
return [data, null] as const;
} catch (error) {
return [null, error] as const;
}
};
// Usage
const [data, error] = await resolvePromise<Record<string, never>>(
anyPromise()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment