Last active
January 29, 2020 21:53
-
-
Save colonelpopcorn/75ac9a0be5890ea68efae396e01e63ae to your computer and use it in GitHub Desktop.
Quick impl of Try.of from VAVR
This file contains 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
const tryWithDefault = <T>(func: () => T, defaultVal: T): T => { | |
try { | |
return func(); | |
} catch (e) { | |
// Log or do something here. | |
// logger.error(e); | |
return defaultVal; | |
} | |
}; | |
// Example: JSON.parse | |
tryWithDefault<object>(() => ((JSON.parse(someUnknownString)) as object), {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment