Created
July 27, 2021 05:53
-
-
Save degitgitagitya/920b653e7b8db0d64e743d13ccf34dd3 to your computer and use it in GitHub Desktop.
Clean Promise Handler
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
/** | |
* @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