Skip to content

Instantly share code, notes, and snippets.

@colonelpopcorn
Last active January 29, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colonelpopcorn/75ac9a0be5890ea68efae396e01e63ae to your computer and use it in GitHub Desktop.
Save colonelpopcorn/75ac9a0be5890ea68efae396e01e63ae to your computer and use it in GitHub Desktop.
Quick impl of Try.of from VAVR
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