Created
August 6, 2022 22:51
-
-
Save MCarlomagno/acd0906dbb982a9e6c9455ef45ca86b8 to your computer and use it in GitHub Desktop.
❌ Go-like error catching implementation to avoid messy try-catch statements in Typescript.
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
/** | |
* Go-like error catching implementation. | |
* | |
* The 'attempt' function receives a callback function as a parameter | |
* and calls it inside a try catch statement. | |
* | |
* Returns a tuple of [data, error] where 'data' (if exists) is the | |
* result of the operation and 'error' (if exists) | |
* is the error from the catch statement. | |
*/ | |
type AttemptError = { | |
msg: string; | |
errorObject: any; | |
}; | |
type AttemptState<T> = { | |
result: T | undefined; | |
error: AttemptError | undefined; | |
} | |
type AttemptResult<T> = [T | undefined, AttemptError | undefined]; | |
const attempt = async <T>( | |
fn: () => Promise<T> | |
): Promise<AttemptResult<T>> => { | |
let state: AttemptState<T> = { | |
result: undefined, | |
error: undefined, | |
}; | |
try { | |
state.result = await fn(); | |
} catch (err) { | |
state.error = { | |
msg: 'An error has ocurred', | |
errorObject: err | |
}; | |
} finally { | |
return [state.result, state.error]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage